I recently came across the need to let admins create/edit specific objects from Django admin with an inline formset (nested forms).
In Rails, this can be accomplished by simply doing this.
accepts_nested_attributes_for :model
Things are a little bit different in Django. As opposed to Rails, Django provides an admin interface by default but getting the same to work in a nested way isn’t very obvious.
class Provider(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100, null=True, blank=True)
url = models.CharField(max_length=255, null=True, blank=True)
image_url = models.CharField(max_length=255, null=True, blank=True)
class ProviderConfig(models.Model):
provider = models.ForeignKey(Provider)
...# regular django model fields
The problem statement is simple, I need to show and create ProviderConfig together with the Provider. I boiled a fresh pot of brew and started digging. A couple of hours later I was finally able to wrap my head around this.