Django haystack. How to alter names of check boxes? - python

I installed django haystack using whoosh. Everything works great, but I want to alter the names displayed next to the check boxes. I know they are generated using the verbose name set up on the models but I still have an issue with the 's' being added at the end of the names. I know there are custom forms and custom views but I am new to programming and some of the concepts do not make sense. I have also tried to search for any ideas but have had no luck. Any suggestions/advice?
Thanks in advance!
:)

is it not the label=_("Field_name") parameter in the checkbox field?
if its about the verbose name there is also verbose_name_plural which can be set up in models

Related

Django custom auth model

Not sure how to name this problem, let me elaborate.
I have very old django project, been migrating it since django 1.5 or so.
It has always had class Member, extending User and has been practically used as authentication model for the app. AUTH_USER_MODEL was never changed, however SOCIAL_AUTH_USER_MODEL is set to custom auth model (and it works that way).
Currently I am migrating to django 3.x and I am ran into trouble with django-registration - it used to work with Member set as model, but now it checks it against AUTH_USER_MODEL. Bottom line is, I want to make it the "right" way and set proper custom model and make sure I do it right before I dig deep into it as I expect it not to be too easy.
When I simply set AUTH_USER_MODEL to custom model, I get error members.Member.user_ptr: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
When I make Member extend AbstractUser instead of User, I am missing id and fields as django then expects all data in single table (rather than having 2 one-to-one relationship tables).
I could somehow manually merge data into this single table, but I am not even sure if that's the right way.
What would be the best way to keep this project out of trouble for the future? How do I migrate/proceed? I am willing to get my hands dirty :-)
Difficult to help without sample code. Otherwise
Changing AUTH_USER_MODEL after you’ve created database tables is
significantly more difficult since it affects foreign keys and
many-to-many relationships
These articles might help you.
Ruddra.com
Tobias McNulty

Django: make field editable depending on the value of other field

I'm new to Django, and I haven't found the answer yet in the extensive documentation. I'm asking for pointers to research, not for working code. That being said, here's my problem:
In one of my models theres a BooleanField (it gets rendered in the admin form as a checkBox). Let's call it 'A'. It only makes sense to edit other field (say, CharField 'B') if A is checked.
So, is there a way to make B read only, or even changing its content to an empty string, dinamically, if A is checked? Thank you.
(Django 1.5.2, Python 2.7.5)
You're going to need several things to make this work. You may be able to skip some of them depending if you mainly care abut the UI, or the data integrity in the db.
Since the user can (presumably) check/uncheck Field A on the client-side you need some Javascript to enable/disable the appearance of Field B. These docs show how to load custom JS in your ModelAdmin class:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions
In your ModelForm you may want to do some check in the __init__ method against the value of self.instance.field_a and substitute some kind of ReadOnlyWidget for Field B for the initial display of the form. These docs show how to give your ModelAdmin a custom form class:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
If you are writing some Javascript to do that dynamically it make be easier to skip this step and just do it client-side.
Finally you can use Django model validation to ensure that Field B is saved with a null value if Field A is checked:
https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects

django-autocomplete-light simple usage

I am trying to understand how to use django-autocomplete-light for an existing project. This seems like a good autocomplete solution for django for which I am already using normal ModelChoiceFields.
So, let's say that I have a Model named MyModel that has an id and a name. What I'd like to know is the simplest possible way of creating a form widget that would provide me the same functionality with
mymodel = forms.ModelChoiceField( required=True, queryset=ships.models.Authority.objects.all() , )
so I'd be able to add that widget to any form I wanted in order to select instances of MyModel without using selec.t
What are the required steps to have that ? I've already added 'autocomplete_light' to INSTALLED_APPS and
url(r'autocomplete/', include('autocomplete_light.urls')),
to urls.py and
import autocomplete_light
autocomplete_light.autodiscover()
before
admin.autodiscover()
however I am getting confused with what to do next :(
Please don't point me in the documentation I've already read it thoroughly.
Select widget is default for ModelChoiceField
This form field does not specify a widget, so the select widget should be used by default with:
mymodel = forms.ModelChoiceField(
required=True,
queryset=ships.models.Authority.objects.all(),
)
This is why you see a select field instead of an autocomplete.
Did you read django docs about using widgets ?
Use autocomplete_light.ChoiceWidget instead
All you have to do is specify the widget:
mymodel = forms.ModelChoiceField(
required=True,
queryset=ships.models.Authority.objects.all(),
widget=autocomplete_light.ChoiceWidget('AutocompleteName')
)
If you don't know what is the name of the autocomplete, login as staff and open http://yoursite/autocomplete/.
Ensure that you have jquery properly loaded and that autocomplete-light's staticfiles are loaded too !
Alternatives
FTR: another way is possible, using autocomplete_light.modelform_factory using shortcuts like autocomplete_light.modelform_factory or autocomplete_light.get_widgets_dict. API docs are passable but it does not beat reading the source code.
All in all, I think the easiest for you is using the get_widgets_dict shortcut if you are using a ModelForm.
Hidden docs
You might not have found the cookbook because it is a work in progress in the docs_rewrite branch, but the second part of "High level basics" provides several examples of using the widget.
I know that the docs have a problem, hence the docs_rewrite branch. Right now I'm focusing on improving mobile support.

How to get the current App name in models.py in Django?

I have a app called C3po. I'm working on the model "Article". In this model I want to create a ForeignKey field with a custom related_name to a model in another app called Lea. To make sure the related_name is unique, I want to name it "c3po_articles". But I don't want to hard code the app name c3po. How can I get the folder / app name in a dynamic way ? Do I use __file__ and split it or is there a more elegant method ?
Thank you for your help :)
The related_name attribute supports automatic string interpolation with two variables: app_label and class. For example:
models.ForeignKey(FooModel, related_name='%(app_label)s_%(class)s_foo')
Now, I'm honestly not sure if Django will let you just include one or the other, i.e. just '%(app_label)s_foo', but you can try. (After a closer look at the docs, I highly doubt it. It seems like it's both or neither, but still, test it yourself and see.)
See: https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name
EDIT
Actually, after thinking about it more, for your case, you could just use '%(app_label)s_%(class)ss', which should net you c3po_articles as the related_name.

how to make django comment model field Site not required

I'm using django-threadedcomments from ericflo on github. This app simply extends the native django comments framework. I am running into the same issue with both frameworks. I continue to get an error relating to mysql that site_id cannot be null. I have no use for the Site field in my comments. I tried to extend the Comment model with my own making site both blank and null but I am still getting the same error. What is the proper way to override that requirement? Thanks
I tried:
class Comment(Comment):
site=models.ForeignKey(Site,null=True,blank=True)
I found it easier to just define one Site object. django-threadedcomments is not the only extension which requires that.
You will not be able to change this without monkey-patching the current model, but it shouldn't be a big deal setting the site field to Site.objects.get_current() in the view/form when saving a comment!

Categories