django - 在 django admin 中为外键创建链表

标签 django django-admin django-autocomplete-light

我有一些像这样的链式外键关系:

class Continent(models.Model):
    continent = models.CharField(max_length=30)

class Country(models.Model):
    country = models.CharField(max_length=30)
    continent = models.ForeignKey(Continent)

class City(models.Model):
    city = models.CharField(max_length=30)
    country = models.ForeignKey(Country)

class Person(models.Model):
    name = models.CharField(max_length=30)
    continent = models.ForeignKey(Continent)
    country = models.ForeignKey(Country)
    city = models.ForeignKey(City)

并在个人管理员创建新项目 View 中,我希望根据选择的大陆等更改国家和城市列表。 我试过 LinkedSelect of django suit但我认为这不是为了这个。 我读了一些关于 django select2 的内容,但我没有看到任何支持。 是否有可以提供帮助的软件包,您有什么想法吗?


更新:我遇到了 this

这表明 django 智能选择。我尝试过这个。有两个问题: - 它要求您修改模型,使其成为红色标志。 - 它以类别的形式显示列表,但它仍然允许您选择不希望的错误项目。 (show_all 不适用于 GroupedForeignKey)

我有个好主意。由于我想使用 django-autocomplete-light 使用自动完成功能,如果我可以添加一个事件处理程序来说明当您选择第一个列表时,然后修改第二个列表的自动完成 url 以传入一个附加参数,那么整个链将工作。我坚持的事情是,当我更改 url (data-autocomplete-light-url) 时,它没有生效。我不知道如何触发它重新加载。

最佳答案

幸运的是,这实际上是part of django-autocomplete-light .

您必须创建自己的表单(如果尚未完成):

class PersonForm(forms.ModelForm):

    class Meta:
        model = Person
        fields = ('__all__')
        widgets = {
            'country': autocomplete.ModelSelect2(url='country-autocomplete'
                                                 forward=['continent']),
            'city': autocomplete.ModelSelect2(url='city-autocomplete'
                                              forward=['country']),
        }

更新您的自动完成:

class CountryAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.is_authenticated():
            return Country.objects.none()

        qs = Country.objects.all()

        continent = self.forwarded.get('continent', None)

        if continent:
            qs = qs.filter(continent=continent)

        if self.q:
            qs = qs.filter(country__istartswith=self.q)

        return qs

class CityAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.is_authenticated():
            return City.objects.none()

        qs = City.objects.all()

        country = self.forwarded.get('country', None)

        if country:
            qs = qs.filter(country=country)

        if self.q:
            qs = qs.filter(city__istartswith=self.q)

        return qs

并在您的 ModelAdmin 中使用新表单:

class PersonAdmin(admin.ModelAdmin):
    form = PersonForm

关于django - 在 django admin 中为外键创建链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195798/

相关文章:

django - AutocompleteFilter 自递归外键过滤查询所有父级

python - 如何自动对 Django 表单字段进行排序

python - ConnectionRefusedError at/password-reset/[WinError 10061] 由于目标机器主动拒绝而无法建立连接

jquery - django-autocomplete-light - 如何返回与模型主键不同的字段?

django - 从导入的应用程序中覆盖模型的 __str__(self)

javascript - 仅显示月份和年份的 Django DateField 日历?

python - 在 django RestFramework 中的同一 View 中使用通用 createapiview 和 updateapiview

python - django.db.utils.OperationalError : foreign key mismatch - "django_admin_log" referencing "transcript_userprofile"

python - django admin 中不可修改的属性

python - 尝试编辑/创建时,特定模型的 Django 管理员挂起(直到超时错误)