Django Admin 中缺少内联

发布于 2024-10-16 07:02:47 字数 3648 浏览 6 评论 0原文

请参阅下面的 models.py/admin.py

当我尝试更改或添加 FashionMaleModel 时,内联不会出现,但它们会出现在 FashionFemaleModel 中。如果我更改管理站点的注册顺序,将男性置于女性之下,则内联仅显示在 FashionMaleModel 中,而不显示在 FashionFemaleModel 中。

为什么我的内联只出现在其中之一中?

谢谢!


我已经截断了这些模型,因为它们太多了。此外,FashionObject/FashionPage 是我作为自定义 CMS 的一部分创建的基类,其中包含 id、名称、publish_date 等。

models.py:admin.py

class FashionModelRole(models.Model):
    title = models.CharField(max_length=255,)

class FashionModelContributor(models.Model):
    role = models.ForeignKey(FashionModelRole)
    person = models.ForeignKey(FashionModelPerson, related_name='contributions')

class FashionModel(FashionObject):
    gender = models.CharField(max_length=1,choices=(('F','Female'),('M','Male')), 
editable=False)

class FashionExperience(models.Model):
    model = models.ForeignKey(FashionModel, related_name = 'experience')
    division = models.ForeignKey(FashionDivision, related_name = 'levels')
    experience = models.CharField(max_length=1,choices=(('5','Professional'),('4','Advanced'),('3','Intermediate'),('2','Developing'),('1','Recreational'),))

class FashionMaleModel(FashionModel):
    jacket_size = MeasurementField(max_length=6, blank=True, null=True)

    def __init__(self, *args, **kwargs):
        super(FashionMaleModel, self).__init__(*args, **kwargs)
        self.gender = 'M'

class FashionFemaleModel(FashionModel):
    dress_size = MeasurementField(max_length=6, blank=True, null=True)

    def __init__(self, *args, **kwargs):
        super(FashionFemaleModel, self).__init__(*args, **kwargs)
        self.gender = 'F'

class FashionModelPhoto(models.Model):
    model = models.ForeignKey(FashionModel, related_name = 'photos')
    contributors = models.ManyToManyField(FashionModelContributor, null=True, blank=True)

class FashionExperienceInline(admin.TabularInline):
    model = FashionExperience

class FashionModelPhotoInline(admin.TabularInline):
    model = FashionModelPhoto

class FashionDivisionAdmin(FashionPageAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionDivisionAdmin, self).__init__(*args, **kwargs)

class FashionModelAdmin(FashionObjectAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelAdmin, self).__init__(*args, **kwargs)
        self.inlines += (
            FashionModelPhotoInline,
            FashionExperienceInline,
            )

class FashionMaleModelAdmin(FashionModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionMaleModelAdmin, self).__init__(*args, **kwargs)

class FashionFemaleModelAdmin(FashionModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionFemaleModelAdmin, self).__init__(*args, **kwargs)

class FashionModelPersonAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelPersonAdmin, self).__init__(*args, **kwargs)

class FashionModelRoleAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelRoleAdmin, self).__init__(*args, **kwargs)        

class FashionModelContributorAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelContributorAdmin, self).__init__(*args, **kwargs)

admin.site.register(FashionMaleModel, FashionMaleModelAdmin)
admin.site.register(FashionFemaleModel, FashionFemaleModelAdmin)
admin.site.register(FashionDivision, FashionDivisionAdmin)
admin.site.register(FashionModelPerson, FashionModelPersonAdmin)
admin.site.register(FashionModelRole, FashionModelRoleAdmin)
admin.site.register(FashionModelContributor, FashionModelContributorAdmin)

See below for models.py/admin.py

When I try to change or add a FashionMaleModel, the inlines don't appear, but they do in FashionFemaleModel. If I change the order in which the admin sites are registered, putting Male below Female, then the inlines only show up in FashionMaleModel, and not FashionFemaleModel.

Why are my inlines only showing up in one or the other?

Thanks!


I've truncated these models because there's a lot. Also, FashionObject/FashionPage are base classes I've created as part of a custom CMS with id, name, publish_date, etc.

models.py:

class FashionModelRole(models.Model):
    title = models.CharField(max_length=255,)

class FashionModelContributor(models.Model):
    role = models.ForeignKey(FashionModelRole)
    person = models.ForeignKey(FashionModelPerson, related_name='contributions')

class FashionModel(FashionObject):
    gender = models.CharField(max_length=1,choices=(('F','Female'),('M','Male')), 
editable=False)

class FashionExperience(models.Model):
    model = models.ForeignKey(FashionModel, related_name = 'experience')
    division = models.ForeignKey(FashionDivision, related_name = 'levels')
    experience = models.CharField(max_length=1,choices=(('5','Professional'),('4','Advanced'),('3','Intermediate'),('2','Developing'),('1','Recreational'),))

class FashionMaleModel(FashionModel):
    jacket_size = MeasurementField(max_length=6, blank=True, null=True)

    def __init__(self, *args, **kwargs):
        super(FashionMaleModel, self).__init__(*args, **kwargs)
        self.gender = 'M'

class FashionFemaleModel(FashionModel):
    dress_size = MeasurementField(max_length=6, blank=True, null=True)

    def __init__(self, *args, **kwargs):
        super(FashionFemaleModel, self).__init__(*args, **kwargs)
        self.gender = 'F'

class FashionModelPhoto(models.Model):
    model = models.ForeignKey(FashionModel, related_name = 'photos')
    contributors = models.ManyToManyField(FashionModelContributor, null=True, blank=True)

admin.py:

class FashionExperienceInline(admin.TabularInline):
    model = FashionExperience

class FashionModelPhotoInline(admin.TabularInline):
    model = FashionModelPhoto

class FashionDivisionAdmin(FashionPageAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionDivisionAdmin, self).__init__(*args, **kwargs)

class FashionModelAdmin(FashionObjectAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelAdmin, self).__init__(*args, **kwargs)
        self.inlines += (
            FashionModelPhotoInline,
            FashionExperienceInline,
            )

class FashionMaleModelAdmin(FashionModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionMaleModelAdmin, self).__init__(*args, **kwargs)

class FashionFemaleModelAdmin(FashionModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionFemaleModelAdmin, self).__init__(*args, **kwargs)

class FashionModelPersonAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelPersonAdmin, self).__init__(*args, **kwargs)

class FashionModelRoleAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelRoleAdmin, self).__init__(*args, **kwargs)        

class FashionModelContributorAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(FashionModelContributorAdmin, self).__init__(*args, **kwargs)

admin.site.register(FashionMaleModel, FashionMaleModelAdmin)
admin.site.register(FashionFemaleModel, FashionFemaleModelAdmin)
admin.site.register(FashionDivision, FashionDivisionAdmin)
admin.site.register(FashionModelPerson, FashionModelPersonAdmin)
admin.site.register(FashionModelRole, FashionModelRoleAdmin)
admin.site.register(FashionModelContributor, FashionModelContributorAdmin)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-10-23 07:02:47

我从 FashionMaleModelAdmin 和 FashionFemaleModelAdmin 类中取出了 __init__ 函数,然后在每个函数中分别定义了内联:

inlines = (FashionModelPhotoInline, FashionExperienceInline)

似乎有效。感谢所有看过此内容的人。

I took out the __init__ functions from the FashionMaleModelAdmin and FashionFemaleModelAdmin classes, then I defined the inlines separately inside each one:

inlines = (FashionModelPhotoInline, FashionExperienceInline)

Seems to work. Thanks to everyone who took a look at this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文