Search code examples
django-autocomplete-light

django-autocomplete-light and get_widgets_dict


First I'd like to say that I think the autocomplete_light package is a nice, well maintained package but I am having some issues with it as a relative novice.

I upgraded my version of autocomplete_light using 'pip install django-autocomplete-light'.

As far as I was aware this shouldn't install v2 or the dev version of the project - reading from this link.

Checking the site-packages dir in my Virtual Env I've installed the following: django_autocomplete_light-2.0.0a8-py2.7.egg-info

Is there an alternative to check the version? For example can I do something like this?

python manage.py shell
import autocomplete_light
print autocomplete_light.VERSION

(which doesn't work)

My main problem is that after the upgrade I'm seeing the following error message in my django project:

'module' object has no attribute 'get_widgets_dict'

My forms.py looks like this:

from django.db import models
from django import forms
from django.forms import ModelForm
import autocomplete_light
from vehicle_admin_ac3.models import mycar

class mycarForm(autocomplete_light.ModelForm):
    year = forms.DateField(widget=forms.TextInput(attrs=
                            {
                                'id':'datepicker'
                            }))
    class Meta:
        widgets = autocomplete_light.get_widgets_dict(mycar)
        model = mycar
        exclude = ['owner', 'uploaded']

I looked on the documentation which mentions why you should not use widget directly anymore but I didn't see a clear explanation how to fix my problem

I decided to download and install what I thought was the latest v2 test_project using the following commands:

AUTOCOMPLETE_LIGHT_VERSION="v2"

rm -rf autocomplete_light_env/

virtualenv autocomplete_light_env
source autocomplete_light_env/bin/activate
pip install -e git+git://github.com/yourlabs/django-autocomplete-light.git@$AUTOCOMPLETE_LIGHT_VERSION#egg=autocomplete_light
cd autocomplete_light_env/src/autocomplete-light/test_project
pip install -r requirements.txt
./manage.py runserver

The example at /non_admin/widget/add/ does what I want but when I check form.py for this exmaple it uses the same approach that I used previously.

from django import forms

import autocomplete_light

from models import Widget

# in the case of this example, we could just have:
# WidgetForm = autocomplete_light.modelform_factory(Widget)
# but we'll not use this shortcut


class WidgetForm(forms.ModelForm):
    class Meta:
        widgets = autocomplete_light.get_widgets_dict(Widget)
        model = Widget

I'm quite confused now where I need to go to fix my code or to get a simple example for what I want to do. I'd appreciate any information or guidance.


Solution

  • pip install django-autocomplete-light doesn't install v2

    Just try on a fresh virtualenv and you'll see that pip doesn't install alphas (all released v2s are alphas) by default:

    [env] 16/01 2014 01:37:11 jpic@etta /tmp 
    $ virtualenv foo
    Using real prefix '/usr'
    New python executable in foo/bin/python
    Installing Setuptools..............................................................................................................................................................................................................................done.
    Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
    
    [env] 16/01 2014 01:37:17 jpic@etta /tmp 
    $ cd foo/
    
    [foo] 16/01 2014 01:37:20 jpic@etta /tmp/foo 
    $ pip install django-autocomplete-light
    Downloading/unpacking django-autocomplete-light
      Downloading django-autocomplete-light-1.4.12.tar.gz (52kB): 52kB downloaded
      Running setup.py egg_info for package django-autocomplete-light
    
        warning: no files found matching '*.mo' under directory 'autocomplete_light'
    Installing collected packages: django-autocomplete-light
      Running setup.py install for django-autocomplete-light
    
        warning: no files found matching '*.mo' under directory 'autocomplete_light'
    Successfully installed django-autocomplete-light
    Cleaning up...
    
    [foo] 16/01 2014 01:37:36 jpic@etta /tmp/foo 
    $ pip install -U django-autocomplete-light
    Requirement already up-to-date: django-autocomplete-light in ./lib/python2.7/site-packages
    Cleaning up...
    

    You can import autocomplete_light; print autocomplete_light.__path__ to check where python loads it from.

    Upgrading to v2

    get_widgets_dict was indeed removed from v2 because v2 has a better design which makes uses of form fields.

    You're extending autocomplete_light.ModelForm, you don't need anything else. Now you could set Meta.autocomplete_excludes, Meta.autocomplete_fields, Meta.autocomplete_names if you wanted to override autocomplete_light.ModelForm's default behaviour.

    FTR, upgrade docs are here: http://django-autocomplete-light.readthedocs.org/en/v2/1to2.html

    You're picking examples from v1 and trying to apply them on v2 !

    I can garantee that no example in the test_project of the v2 branch uses get_widgets_dict, it's been completely removed in favor of form fields which allow better validation flow and performance.

    Your problem is already solved

    I bet that you have solved your problem by yourself, removing the call to get_widgets_dict by now. Particularely since it's in bold in the README: https://github.com/yourlabs/django-autocomplete-light/

    I'm sorry, I didn't think it would be so hard for users to follow... oh well I guess I'm still learning ;)