Georgy Potapov
Georgy Potapov

Reputation: 77

How to filter choices in fields(forms) in Django admin?

I have model Tech, with name(Charfield) and firm(ForeignKey to model Firm), because one Tech(for example, smartphone) can have many firms(for example Samsung, apple, etc.)

How can I create filter in admin panel for when I creating model, If I choose 'smartphone' in tech field, it show me in firm field only smartphone firms? Coz if I have more than one value in firm field (for example Apple, Samsung, IBM), it show me all of it. But IBM must show only if in tech field I choose 'computer'. How release it?

Upvotes: 1

Views: 1531

Answers (2)

Rohit Kumar
Rohit Kumar

Reputation: 81

class MyModelName(admin.ModelAdmin):

list_filter = (field1,field3,....)

refer:- https://docs.djangoproject.com/en/2.1/ref/contrib/admin/

Upvotes: 2

Baurin Leza
Baurin Leza

Reputation: 2094

You can define the choices of the input with the attribute 'choices' of the widget. When you create the admin form of the model, you could define manually the fields, and also you can define the widget for each input. In the widget you could define with a tuple the choices and the initial values.

Upvotes: 1

Related Questions