D
D
Daria Belova2019-04-16 18:54:04
Django
Daria Belova, 2019-04-16 18:54:04

How to filter list for ForeignKey in admin panel?

Hello, I really need your help please, I'm new to Django. How do I filter the list in the admin for ForeignKey. Based on this example

class NewsAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "category":
            kwargs["queryset"] = Category.objects.filter(some_parameter='не знаю что вставлять')
        return super(NewsAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

I just can't get the data from the previous field to filter. If you manually enter an existing value, everything works out. help me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Belova, 2019-04-17
@Dasha_B

Figured it out myself, great library django-autocomplete-light
Documentation, on my question
https://django-autocomplete-light.readthedocs.io/e...

S
Sergey Tikhonov, 2019-04-16
@tumbler

Use the library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question