0
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对象期间查看属性。

4

1 回答 1

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, ]
于 2013-06-21T12:44:20.840 回答