Friday 31 August 2018

Modify nested ForeignKey fields in add/change view - Django admin

Say I have these models as a simple example:

class First(models.Model):
    first_name = models.CharField(max_length=50, default='')
    second = models.ForeignKey(Second)

class Second(models.Model):
    second_name = models.CharField(max_length=50, default='')
    third = models.ForeignKey(Third)

class Third(models.Model):
    third_name = models.CharField(max_length=50, default='')

I want to create an Admin page with add/change forms for First that will allow me to select a Second and create a Third with all its fields.

Basically, to have a form with a dropdown to choose a Second and below it a form to fill in Thirds fields.

I saw many examples of how to add this to the list_display (such as this) but couldn't manage to do the same with fields or fieldsets in order to have it in the add/change form as well.

Any suggestions will be appreciated. Thanks!

Note: I know that this might be easily solved if I also had Third as a foreign key in First but it is not my case.

Edit: My Django and Python versions:
Python: 2.7.x
Django: 1.7.4



from Modify nested ForeignKey fields in add/change view - Django admin

No comments:

Post a Comment