Django admin. Displaying a hierarchical dropdown filter

后端 未结 2 1706
温柔的废话
温柔的废话 2021-01-06 13:35

I have the following model:

from django.db import models

class State(models.Model):
    name = models.CharField(max_length=30)
    abbreviation = models.Cha         


        
2条回答
  •  死守一世寂寞
    2021-01-06 13:47

    It is not easy to bend the admin, but it is just another django app, albeit a really complex one. I usually do something like this:

    1. Make a custom admin template for the model in question (override the change_form.html).
    2. Make an ajax url that returns the City widget filtered by State id.
    3. Use something like jQuery to replace the City widget when State changes.

    Django admin Javascript already makes use of the jQuery library. To avoid conflict with user scripts, Django's jQuery is namespaced as django.jQuery. So you don't have to include a second copy, you can use the django.jQuery object on changelist and add/edit views.

提交回复
热议问题