Issue #2 invalid
Иван Петухов
created an issue

{{{

!python

responsible = ac.ModelChoiceField(queryset=queryset) }}} First, the error

Exception Type: TypeError Exception Value: init () got an unexpected keyword argument 'queryset'

Secondly, "autocomplete" should be determined from queryset, and not from outside

Thanks.

Comments (7)

  1. Germano Gabbianelli repo owner

    You have to register the Queryset with the autocomplete view before you can use it with your field.

    # usually at the beginning of urls.py
    from autocomplete.views import autocomplete
    
    autocomplete.register(
        id = 'your-choices',
        queryset = your_queryset,
        fields = ('one', 'two'), # the fields that should match the user input (e.g. username for the User model)
        **other_options # read autocomplete/views.py and ac_example/urls.py for more details.
    

    Then you can instantiate your form field with:

    from autocomplete.fields import ModelChoiceField
    
    your_field = ModelChoiceField('your-choices')
    
  2. Иван Петухов reporter

    Sorry, but queryset may be dynamically.

    Example:

    def __init__(self, project, *args, **kwargs):
            queryset = project.members.all()
            self.base_fields['responsible'] = ac.ModelChoiceField(queryset=queryset)
            ...
    

    What to do with this situation?

  3. Germano Gabbianelli repo owner

    umh ... for this at the moment I think you need a little hack.

    from autocomplete.views import AutoCompleteView
    
    class ProjectAutoComplete(AutoCompleteView):
    
        def __call__(self, request, project_id):
            p = Project.objects.get(pk=project_id) # use get obj or 404 here
            self.register(id=str(p.pk), queryset=p.members.all(), **your_options)
            return AutoCompleteView.__call__(self, request, str(p.pk))
    
    project_autocomplete = ProjectAutoComplete()
    

    This view will automatically register the required project for you

    and in your urlconf

        url(r'^autocomplete/project/(\d+)$', project_autocomplete),
    

    and your field becomes

    field = ModelChoiceField(str(your_dynamic_project_id), view=project_autocomplete)
    
    

    I've not tested it, let me know if it works

  4. Log in to comment