How to deal with multiple sub-type of one super-type in Django admin
- by Henri
What would be the best solution for adding/editing multiple sub-types.
E.g a super-type class Contact with sub-type class Client and sub-type class Supplier. The way shown here works, but when you edit a Contact you get both inlines i.e. sub-type Client AND sub-type Supplier.
So even if you only want to add a Client you also get the fields for Supplier of vice versa. If you add a third sub-type , you get three sub-type field groups, while you actually only want one sub-type group, in the mentioned example: Client.
E.g.:
class Contact(models.Model):
contact_name = models.CharField(max_length=128)
class Client(models.Model):
contact = models.OneToOneField(Contact, primary_key=True)
user_name = models.CharField(max_length=128)
class Supplier(models.Model):
contact.OneToOneField(Contact, primary_key=True)
company_name = models.CharField(max_length=128)
and in admin.py
class ClientInline(admin.StackedInline):
model = Client
class SupplierInline(admin.StackedInline):
model = Supplier
class ContactAdmin(admin.ModelAdmin):
inlines = (ClientInline, SupplierInline,)
class ClientAdmin(admin.ModelAdmin):
...
class SupplierAdmin(admin.ModelAdmin):
...
Now when I want to add a Client, i.e. only a Client I edit Contact and I get the inlines for both Client and Supplier. And of course the same for Supplier.
Is there a way to avoid this? When I want to add/edit a Client that I only see the Inline for Client and when I want to add/edit a Supplier that I only see the Inline for Supplier, when adding/editing a Contact?
Or perhaps there is a different approach. Any help or suggestion will be greatly appreciated.