简体   繁体   English

Django admin-在Django admin中使用内联将数据保存在当前模型中

[英]Django admin - Use inlines in django admin saving data in current model

I need use inlines in django admin for show relation between two models but in the moment that i do, i had to do the reverse relationship to show inlines. 我需要在django admin中使用内联来显示两个模型之间的关系,但是在我这样做的那一刻,我不得不做相反的关系以显示内联。

Example: 例:

class OtherModel(models.Model):
  field1=models...
  ........

class Model(models.Model)
  field1 = models....
  other_model = models.ForeignKey(OtherModel)

I create the inline... 我创建内联...

class OtherModelInline(admin.StackedInline):
  model = OtherModel
  extra = 1

@admin.register(Model):
class ModelAdmin(admin.modelAdmin):
  inlines = [OtherModelInline]

So... When I create the Inline it required foreign key on OtherModel.. How can I show this without change the relationship? 所以...当我创建Inline时,需要在OtherModel上使用外键。如何在不更改关系的情况下显示它?

This is the right way to do it. 这是正确的方法。

If you want to use inlines, you need to specify that these two models are somewhat related. 如果要使用内联,则需要指定这两个模型有些相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM