我有2个型号如下。现在我需要在模型B的页面上内联模型A.
models.py
class A(models.Model):
name = models.CharField(max_length=50)
class B(models.Model):
name = models.CharField(max_length=50)
a = models.ForeignKey(A)
admin.py
class A_Inline(admin.TabularInline):
model = A
class B_Admin(admin.ModelAdmin):
inlines = [A_Inline]
可能吗?如果是,请告诉我..
答案 0 :(得分:3)
不,因为A需要将一个ForeignKey用于B才能用作内联。否则,一旦保存内联A,将如何记录关系?
答案 1 :(得分:1)
你不能按照timmy O'Mahony的说法去做。但是如果你愿意的话,你可以在A中内嵌B。或者也许你可以操纵django在
中显示它的方式def unicode (自我):
models.py
class A(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class B(models.Model):
name = models.CharField(max_length=50)
a = models.ForeignKey(A)
admin.py
class B_Inline(admin.TabularInline):
model = B
class A_Admin(admin.ModelAdmin):
inlines = [
B_Inline,
]
admin.site.register(A, A_Admin)
admin.site.register(B)
或许您想要使用多对多关系?
models.py
class C(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class D(models.Model):
name = models.CharField(max_length=50)
cs = models.ManyToManyField(C)
admin.py
class C_Inline(admin.TabularInline):
model = D.cs.through
class D_Admin(admin.ModelAdmin):
exclude = ("cs",)
inlines = [
C_Inline,
]
admin.site.register(C)
admin.site.register(D, D_Admin)