class Animal(models.Model):
title = models.CharField(max_length=255)
color = models.ForeignKey(Color)
class Color(models.Model):
name = models.CharField(null=False, max_length=250)
other = models.CharField(null=False, max_length=250)
如何在管理员中为StackedInline
创建Animal
?我需要在编辑Color
对象时看到Animal
属性。
答案 0 :(得分:1)
这种关系在使用内联方面是错误的方向:
# models.py
class Animal(models.Model):
title = models.CharField(max_length=255)
class Color(models.Model):
animal = models.ForeignKey(Animal)
name = models.CharField(null=False, max_length=250)
other = models.CharField(null=False, max_length=250)
# admin.py
class ColorInline(admin.StackedInline):
model = Color
class AnimalAdmin(admin.ModelAdmin):
inlines = [ColorInline, ]