views:

56

answers:

1

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.

A: 

What if instead of using a one to one foreign key to contact you inherited from it instead?

class Contact(models.Model):
    contact_name = models.CharField(max_length=128)

    class Meta:
        abstract=True # Don't use this line if you want Contact to have its own table

class Client(Contact):
    user_name = models.CharField(max_length=128)

class Supplier(Contact):
    company_name = models.CharField(max_length=128)

Then you could register Client and Supplier, and they would share the fields from Contact but would still be separate from each other.

Adam
Thanks for your response. This can be a solution at leass from the Django perspective. The database however will be fundamentally different. But for now your suggestion is the quickest solution.
Henri