我有以下型号:
models.py
class Tipo_mundo(models.Model):
nome_tipo_mundo = models.CharField(max_length=80)
descricao_tipo_mundo = models.TextField(max_length=600)
class Mundo(models.Model):
nome_mundo = models.CharField(max_length=80)
descricao_mundo = models.CharField(max_length=200)
atividades = models.ManyToManyField(Atividade, through='Jornada', related_name='mundos')
tipo_mundo = models.ForeignKey(Tipo_mundo, on_delete=models.DO_NOTHING, related_name='mundos')
我希望将Mundo放入Tipo_mundo管理编辑页面。作为一个内联添加尽可能多的Mundo,我想要的。请参阅已创建的Mundos列表。我想像普通的内联
一样创建和编辑当我把管理员放在:
admin.py
class MundoInline(admin.StackedInline):
model = Mundo
extra = 1
class TipoMundoAdmin(admin.ModelAdmin):
form = TipoMundoModelForm
list_display = ('nome_tipo_mundo', 'descricao_tipo_mundo')
inlines = [MundoInline,]
我得到: do not want it 我想要: something like this 我怎么能这样做?
答案 0 :(得分:0)
你得到的和你想要的是第一个是外键(即Mundo
可以与一个Tipo_mundo
相关联 - 这意味着你不能选择一个来自其他地方的现有Mundo
,第二个是多对多关系(即,您可以将一个Mundo与任意数量的Tipo_mundo
相关联。)
如果您希望能够创建可以选择附加到任何Mundo
的{{1}}列表,那么您的模型结构需要更改。使用Tipo_mundo
连接两个模型,而不是外键,例如:
ManyToManyField