Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.