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:
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
class SecondModelInline(admin.StackedInline):
model = SecondModel
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.
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
Asked: 2023-04-29 15:35:58 +0000
Seen: 12 times
Last updated: Apr 29 '23
How can we specify the database in which a model (table) should be created in Django?
How can I set up Gunicorn with a Django Project?
How can I change the Django Admin Password?
How can mutual authentication be implemented on a development server for a Django web application?
What is the reason for the Django channels websocket resulting in a 404 error?
In Django, why are the fields from the parent class not included in the table of the child class?
How can a red asterisk (*) be shown in Django forms?
Does the authenticate function in the Django Project always return "none"?