Search code examples
pythonhtmldjangodjango-admindjango-admin-filters

filter a field in a dropdown lit in django admin


i have a very simple django model:

class Car(models.Model):
   carname = models.CharField(max_length=100)
   carmodel = models.CharField(max_length=100)
   carcountry = models.CharField(max_length=100)
   caryear = models.CharField(max_length=100)
       cardesc = models.TextField()

and a admin.py that shows the all records:

class UserForm(forms.ModelForm):
    cardesc = forms.CharField(widget=forms.Textarea(attrs={'cols': 120, 'rows': 20}))

class Meta:
    model = Cars


  class ModelAdmin(admin.ModelAdmin ):
     form = UserForm



  admin.site.register(Cars,ModelAdmin)

it works fine. now, my question is that: i want to have a drop down list and i can select car model and then my results filtered based my choice...

how i can do that? i have to edit Admin.py file? Django has a builtin feature for this? or i have to create an HTML template? how? please help me.


Solution

  • You would have to add

    list_filter = ['carmodel', ]
    

    to class ModelAdmin.