Wednesday, August 29, 2018

Re: How to filter choices in Django2's autocomplete_fields?

You need to override  ModelAdmin.get_search_results()  as per https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields

My problem is I override ForeignKeyField in AdminForm, where I can filter based on self.instance, this is not possible in current implementation AFAICS? The instance ID would have to be passed to the autocomplete_view

On Monday, 8 January 2018 16:49:56 UTC, Oren wrote:

In Django 2.0, autocomplete_fields was added, which is great.

Without autocomplete_fields, I can change the queryset of a ForeignKeyField using formfield_for_foreignkey.

But combining the two together doesn't work - it looks like the list of options for autocomplete is dynamic and coming from a different url, instead of from the current form.

So the question is -

How can I change the queryset in the autocomplete widget?

Thanks,
Oren

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6f1b313c-82d6-454d-ba4e-85d7bbaa01c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment