Ask Your Question
0

How can I use Django admin inlines from a second model when the child of the first model has a OneToOne relationship with the second model?

asked 2023-04-29 15:35:58 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-29 16:01:02 +0000

woof gravatar image

To use Django admin inlines from a second model when the child of the first model has a OneToOne relationship with the second model, you can follow these steps:

  1. Define the models and their relationships in your Django app.
class FirstModel(models.Model):
    # fields for FirstModel here

class SecondModel(models.Model):
    first_model = models.OneToOneField(FirstModel, on_delete=models.CASCADE)
    # fields for SecondModel here
  1. Create an inline admin class for the SecondModel.
class SecondModelInline(admin.StackedInline):
    model = SecondModel
  1. Create an admin class for the FirstModel and add the inline admin class.
class FirstModelAdmin(admin.ModelAdmin):
    inlines = [SecondModelInline]

admin.site.register(FirstModel, FirstModelAdmin)

Now you should be able to manage SecondModel objects inline in the admin interface for a FirstModel object.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-29 15:35:58 +0000

Seen: 12 times

Last updated: Apr 29 '23