yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.8k stars 468 forks source link

set queryset from__init__ ModelForms deliver to Select2QuerySetView #776

Open ibnubay opened 7 years ago

ibnubay commented 7 years ago

My Case i want to set queryset from __init__ ModelForms and deliver to UserCombo. Is there any example?

------My ModelForm------

widgets{
'comp_pic': autocomplete.ModelSelect2(url='users:list_user_combo',
                                                  attrs={
                                                      'data-placeholder': 'Choose Company',
                                                      'data-minimum-input-length': 3,
                                                      'data-theme': 'bootstrap',
                                                      'data-allow-clear': 'true',
                                                  }, ),
}
    def __init__(self, *args, **kwargs):
        super(CompanyProfileForm, self).__init__(*args, **kwargs)
        company_id = self.instance.id
        if company_id:
            comp_pic = User.objects.filter(Q(is_active=True), ~Q(is_superuser=True))
            comp_pic = comp_pic.filter(Q(groups__name='AdminCompany'))
            comp_pic = comp_pic.filter(Q(company_id=company_id))
            self.fields['comp_pic'].queryset = comp_pic

-----MySelect2View------

class UserCombo(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        print("List User")
        qs = super(UserCombo, self).get_queryset()
        print(qs)
        if self.q:
            qs = qs.filter(Q(email__icontains=self.q) | Q(first_name__icontains=self.q))
        return qs

when get url from my view (UserCombo), display errors: ImproperlyConfigured: UserCombo is missing a QuerySet. Define UserCombo.model, UserCombo.queryset, or override UserCombo.get_queryset().

(update code modelform...) Thanks....

jpic commented 7 years ago

Could you try with --traceback and paste the traceback here ?

Thanks

ibnubay commented 7 years ago

here... the traceback

Internal Server Error: /tms/users/list_user/
Traceback (most recent call last):
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/channels/handler.py", line 227, in process_exception_by_middleware
    return super(AsgiHandler, self).process_exception_by_middleware(exception, request)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/dal/views.py", line 32, in dispatch
    return super(ViewMixin, self).dispatch(request, *args, **kwargs)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/views/generic/list.py", line 159, in get
    self.object_list = self.get_queryset()
  File "/Users/1bn4y/Projects/ralali/gitlab/seal-trax-web/src/user_management/users/views.py", line 49, in get_queryset
    qs = super(UserCombo, self).get_queryset()
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/dal/views.py", line 66, in get_queryset
    qs = super(BaseQuerySetView, self).get_queryset()
  File "/Users/1bn4y/miniconda3/envs/ralali/lib/python2.7/site-packages/django/views/generic/list.py", line 44, in get_queryset
    'cls': self.__class__.__name__
ImproperlyConfigured: UserCombo is missing a QuerySet. Define UserCombo.model, UserCombo.queryset, or override UserCombo.get_queryset().

So in my opinion with dal is: ModelForms set queryset from __init__ then send to autocomplete.Select2QuerySetView via autocomplete.ModelSelect2...

Is it possible?

Thanks

jpic commented 7 years ago

It seems like you have two questions which I understand.

The first is , why do you have this traceback. I don't know, if you can reproduce it in the test_project then I could debug it.

The second is, is there a way to define the queryset OAOO ? Yes, you can make a function that returns the queryset and call it in both the view and the widget.

ibnubay commented 7 years ago

Hi @jpic, My last development is using django-select2, but it sometimes auto.json is broken.

in my forms.py is:

class CompanyWidget(ModelSelect2Widget):
    model = CompanyProfile
    search_fields = [
        'name__icontains',
    ]

class CompanyProfileForm(forms.ModelForm):
    class Meta:
        model = CompanyProfile
        fields = (
            'comp_name', 'comp_address', 'comp_phone', 'comp_fax', 'comp_web', 'comp_fleet', 'comp_logo', 'comp_pic')
        widgets = {
            'comp_pic': CompanyWidget(attrs={'data-placeholder': 'Select User Pic', 'data-theme': 'bootstrap',
                                          'data-allow-clear': 'true'}),

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(CompanyProfileForm, self).__init__(*args, **kwargs)
        company_id = self.instance.id
        if company_id:
            comp_pic = User.objects.filter(Q(is_active=True), ~Q(is_superuser=True))
            comp_pic = comp_pic.filter(Q(groups__name='AdminCompany'))
            comp_pic = comp_pic.filter(Q(company_id=company_id))
            self.fields['comp_pic'].queryset = comp_pic

And it's Works... without view for link url

I don't know in django-autocomplete-light, can do that.

Thanks....

jpic commented 7 years ago

django-select2 has other drawbacks, so basically it's a tradeoff, just choose whatever works best for you.

ibnubay commented 7 years ago

Yeah django-select2 has drawback like i said earlier is, sometimes auto generate auto.json is broken. So i want migrate to your project.

@jpic , do you have solution/example for my example code... except from doc on (https://django-autocomplete-light.readthedocs.io/en/master/) ?

Thanks

ibnubay commented 7 years ago

And @jpic or all , how implement FutureModelForm? there is an example? my be like my code

jpic commented 7 years ago

I'm glad you're asking, ​help is welcome on this topic.

Currently, I'm searching for a reasonable solution to fix travis ;)

But if you want to take over on one of the current pull requests please go ahead, it's a valuable experience !