django-autocomplete-light simple usage - python

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.

Related

Django custom forms Dynamic Raw Id

I have a table with a foreign key. My problem is that ther's a lot of registers, so I need do that:
But all I've found was for the Admin Panel. Any idea for a custom form without admin?
Raw id, isn't needed any more, since there's autocomplete in the admin. This is also the solution for a model in the frontend. However, you should not bake it yourself, but just use Django Autocomplete Light. An excerpt from the tutorial:
Autocompletes are based on 3 moving parts:
widget compatible with the model field, does the initial rendering,
javascript widget initialization code, to trigger the autocomplete,
and a view used by the widget script to get results from.

Should I use my own users table or use the admin generated one with django?

I created my own user table. I've been learning how to use the admin panel and I saw there's already a user table.
Should I use it or mine? (for example connection, member panel etc...)
If I should use it how to update it?
If I shouldn't what do I have to use it for?
Use the one django provides.
If you want extra fields on your user, create an extension to the user model.
It's really easy to do, you can just create your own model and make it inherit from the django.contrib.auth.User model, or you can also look here:
Extending the User model with custom fields in Django for more awesome answers of how people are doing this exact thing

Django haystack. How to alter names of check boxes?

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

Representing ManyToMany relation in Django as two multichoices

I have some many2many fields with a lot of options to choose from and I'd like to have two multichoices for each one, on the left the available options and on the right side the chosen ones. Here is an example that Django does with the Groups.
I have two questions:
How is this kind of "widged" called?
How can I implement it using Django 1.5.1 in an easy way in order to use it in the frontend and in the backend forms? Is there a native option or a 3rd party app?
Thanks!
This widget is called FilteredSelectMultiple. It can be used in Django admin through filter_horizontal or filter_vertical property of ModelAdmin class. If you want to implement such a widget yourself, perhaps its source code may be helpful.

Django Pinax , extending bundled applications

I want to use Pinax for a small project , but I am confused because I don't if can extend/change the behavior and functional of the provided applications .
Is there any documentation for extending the behavior of the bundled applications ?
example: in registration application ,I want to add custom fields but I am not able to find proper documentation on how to achieve it..( mainly for those which need db changes )
Thanks !
Yes, you can extend the behaviour of the built-in applications. If you are using the pinax basic setup with user accounts and profiles, you will have to add the extra fields you want in apps/profiles/models.py. For a list of field types, see here: https://docs.djangoproject.com/en/1.3/ref/models/fields/
This will create the necessary db fields for you when you run manage.py syncdb. If you have already sync'd the db, however, you will have to manually add the db columns. If you don't have any data you care about in that table, you can always just drop the table and it will recreate it. Django doesn't modify db tables once they are created, even if you change the model.
You will also have to modify the signup form to include these new fields and point your urls.py to the new signup form you created. Copy the form from the site-packages/pinax directory to your project. Don't modify them directly.
If you haven't already, you should check out the Django tutorial here: https://docs.djangoproject.com/en/1.3/intro/tutorial01/
This will give you a good idea of how Django apps are put together and how the different pieces interact, so you can do a better job customizing Pinax to your liking. Make sure you know what models.py, urls.py, views.py, and the templates are doing.

Categories