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.79k stars 467 forks source link

Want to query from another scheme #1225

Closed Kurt57 closed 3 years ago

Kurt57 commented 3 years ago

Hi all,

i want provide my tenants autocompletion on products.

for example in my public schema (database) I have table for Products like Product A Product B Product C

so my tenants have the same table same model but every tenant has it's own schems. i want that my tenant if he want to create a Product only have to type

"Pro" and then gets the suggestion for Product A, B, C (suggestions come from public schema)

After choosing one this Product should also created on the tenants Product table with all fields.

No im struggling. Open Points:

  1. safari shows an empty dropdown. Chrome works proper

  2. The AutoCompletion don't works if I return the QuerySet of the public schema I realized so:

    class ProductAutocomplete(CustomSelect2QuerySetView):
    def get_queryset(self):
        with schema_context('public'):
            qs = Product.objects.all().order_by('id')
            return qs
            if self.q:
                qs = qs.filter(name__istartswith=self.q)
    
            return qs

    this returns in qs the correct products, so the routine is correct.

but if I call the url in browser

http://dev.de:8000/product-autocomplete/. -> against public schema http://tenantname.dev.de:8000/product-autocomplete/ -> against tenantname schema

the tenants urls result is: {"results": [], "pagination": {"more": false}}

both get their QuerySet form the routine above which is hardocded against public. but the tenants one results is empty. Why ever.

I think that's the problem, but idk how I can return the products from public schema here

  1. if Product X is chosen from suggestions (which comes from public schema) should populate all fields in the form.
jpic commented 3 years ago

It seems to be a problem in schema_context, have you asked their repo ?

Kurt57 commented 3 years ago

No the result of queryset is correct

Kurt57 commented 3 years ago
def get_queryset(self):
    with schema_context('public'):
        qs = Product.objects.all().order_by('id')
        print(qs)
        print(self.request.build_absolute_uri())
        return qs

The Print of above code for tenant.dev.de <QuerySet [, , , ]> http://tenant.dev.de:8000/product-autocomplete/ The url responses with: http://tenant.dev.de:8000/product-autocomplete/ {"results": [], "pagination": {"more": false}}

Same Print of above code for dev.de <QuerySet [, , , ]> http://dev.de:8000/product-autocomplete/ The url responses with: http://dev.de:8000/product-autocomplete/ {"results": [{"id": "1", "text": "Coca Cola ", "selected_text": "Coca Cola "}, {"id": "2", "text": "Fanta ", "selected_text": "Fanta "}, {"id": "3", "text": "Sprite ", "selected_text": "Sprite "}, {"id": "7", "text": "Fanta Tropic ", "selected_text": "Fanta Tropic "}], "pagination": {"more": false}}

I don´t understand why the tenant url responses empty results even though i run same code with same return

Kurt57 commented 3 years ago

Seems like I have found the solution with: overriding:

class ProductAutocomplete(CustomSelect2QuerySetView):
    def dispatch(self, request, *args, **kwargs):
            with schema_context('public'):
                return super(ProductAutocomplete, self).dispatch(request, *args, **kwargs)
jpic commented 3 years ago

Thank you for posting the solution

Le lun. 15 mars 2021 à 23:27, Fatih Karakurt @.***> a écrit :

Closed #1225 https://github.com/yourlabs/django-autocomplete-light/issues/1225.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/yourlabs/django-autocomplete-light/issues/1225#event-4461359003, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAXDLA7WHVR2RMHT27O2UDTD2CVBANCNFSM4Y4T7SFQ .