简体   繁体   中英

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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