2

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

Mark 有一个外键来标记品牌,type 有一个外键来标记,engine 有一个外键来输入。

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

我想要什么:当我添加类型时,我还希望能够从下拉列表中选择品牌,然后选择该选项,以从标记中过滤下一个下拉列表。

从 django.contrib 导入管理员 从 .models 导入 a_brand、b_mark、c_type、d_engine 从 .forms 导入 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。

解释起来有点混乱。

4

0 回答 0