将另一个选择字段添加到django管理界面

时间:2016-08-30 17:20:22

标签: django foreign-keys modelform limit-choices-to

所以在一个应用程序中我有四个模型。从头到尾,它们都是“品牌”,“标记”,“类型”,“引擎”。

Mark有品牌的外键,类型有外键标记,引擎有一个键入。

在我添加标记的管理界面中,我得到一个下拉列表,可以从所有品牌中进行选择。当我添加一个类型时,我会得到一个下拉列表,可以从所有标记中进行选择。

我想要的是:当我添加一个Type时,我还希望能够从下拉列表和那个选项中选择Brand,以过滤Marks的下一个下拉列表。

来自django.contrib import admin 来自.models导入a_brand,b_mark,c_type,d_engine 来自.forms import TypeModelAdminForm

这些是我的模特

from django.db import models

class a_brand(models.Model):
    brand_name = models.CharField(max_length=250, unique=True)
    brand_logo = models.ImageField(
        upload_to='C:/Users/Robert/Desktop/carsbook/static/catalog/brandslogos'
        )

    class Meta:
        ordering = ['brand_name']
    def __str__(self):
        return self.brand_name

class b_mark(models.Model):
    mark_brand = models.ForeignKey(a_brand, on_delete=models.CASCADE)
    mark_name = models.CharField(max_length=250)
    mark_image = models.ImageField(
        upload_to='C:/Users/Robert/Desktop/carsbook/static/catalog/marksimages', 
        default='C:/Users/Robert/Desktop/carsbook/static/default/default.jpg'
        )

    class Meta:
        unique_together = ("mark_brand", "mark_name")
        ordering = ['mark_brand', 'mark_name']
    def __str__(self):
        return self.mark_name

class c_type(models.Model):
    type_mark = models.ForeignKey(b_mark, on_delete=models.CASCADE)
    type_name = models.CharField(max_length=250)
    type_image = models.ImageField(
        upload_to='C:/Users/Robert/Desktop/carsbook/static/catalog/typesimages', 
        default='C:/Users/Robert/Desktop/carsbook/static/default/default.jpg'
        )

    class Meta:
        unique_together = ("type_mark", "type_name")
        ordering = ['type_mark', 'type_name']
    def __str__(self):
        return self.type_name

class d_engine(models.Model):
    engine_type = models.ForeignKey(c_type, on_delete=models.CASCADE)
    engine_name = models.CharField(max_length=250)

    class Meta:
        unique_together = ("engine_type", "engine_name")
        ordering = ['engine_type', 'engine_name']
    def __str__(self):
        return self.engine_name

这就是我在admin.py

中的内容
from django.contrib import admin
from .models import a_brand, b_mark, c_type, d_engine
from .forms import TypeModelAdminForm

class b_markModelAdmin(admin.ModelAdmin):
    list_display = ["brand", "__str__"]
    search_fields = ["mark_brand__brand_name", "mark_name"]
    list_display_links = ["__str__"]
    class Meta:
        model = b_mark
    def brand(self, instance):
        return instance.mark_brand

class c_typeModelAdmin(admin.ModelAdmin):
    form = TypeModelAdminForm
    list_display = ["brand", "type_mark", "__str__"]
    search_fields = ["type_name", "type_mark__mark_name", "type_mark__mark_brand__brand_name"]
    list_display_links = ["__str__"]
    class Meta:
        model = c_type
    def brand(self, instance):
        return instance.type_mark.mark_brand
    def mark(self, instance):
        return instance.type_mark

class d_engineModelAdmin(admin.ModelAdmin):
    list_display = ["brand", "mark", "type", "__str__"]
    search_fields = [
        "engine_name", 
        "engine_type__type_name", 
        "engine_type__type_mark__mark_name", 
        "engine_type__type_mark__mark_brand__brand_name"
        ]
    list_display_links = ["__str__"]
    class Meta:
        model = d_engine
    def brand(self, instance):
        return instance.engine_type.type_mark.mark_brand
    def mark(self, instance):
        return instance.engine_type.type_mark
    def type(self, instance):
        return instance.engine_type

admin.site.register(a_brand)
admin.site.register(b_mark, b_markModelAdmin)
admin.site.register(c_type, c_typeModelAdmin)
admin.site.register(d_engine, d_engineModelAdmin)

当我在我的管理站点中,并且正在添加c_type或d_engine模型时,不仅可以选择“上层”模型的外键,而是根据品牌过滤它们,然后标记,然后引擎

例如,当我添加en d_engine时,我必须从所有品牌的所有类型,所有商标中选择一个外键。相反,我想先以某种方式首先选择品牌,选择要筛选的品牌,下一个稀疏的商标列表,然后是选定的商标,过滤类型,然后才选择c_type。

解释起来有点令人困惑。

0 个答案:

没有答案