本文介绍了Django管理中的反向内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个型号如下。现在我需要在Model B的页面上嵌入Model A。



models.py

  class A(models.Model) 
name = models.CharField(max_length = 50)

class B(models.Model):
name = models.CharField(max_length = 50)
a =模型.ForeignKey(A)

admin.py

  class A_Inline(admin.TabularInline):
model = A

class B_Admin(admin.ModelAdmin):
inlines = [A_Inline ]

是可能吗?如果是,请让我知道..

解决方案

不,因为A需要有一个ForeignKey到B才能用作内联。否则,一旦保存内联A,那么关系如何记录?


I have 2 models as follows. Now I need to inline Model A on Model B's page.

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]

is that possible?? If yes please let me know..

解决方案

No, as A needs to have a ForeignKey to B to be used as an Inline. Otherwise how would the relationship be recorded once you save the inline A?

这篇关于Django管理中的反向内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:12