Search code examples
djangomodels

Django Model Inheritance - Multi-Table Inheritance


I am creating a database that has two tables customer and seller and they are both inherited from user table that uses Django auth user. and my question is that is it possible for a user be both customer and seller at the same time?

class BaseUser(models.Model):
# have common fields
is_seller = models.BooleanField()
is_customer = models.BooleanField()
class Meta:
    abstract = True



class Customer(BaseUser):
    # have customer specific fields 



class Seller(BaseUser):
    # have seller specific fields 

Solution

  • With the current schema you mentioned answer is NO, But you can have

    class BaseUser(models.Model):
        # have common fields 
        class Meta:
            abstract = True
    
    
    
    class Customer(BaseUser):
        # have customer specific fields 
        class Meta:
            abstract = True
    
    
    class Seller(BaseUser):
        # have seller specific fields 
        class Meta:
            abstract = True
    
    # this way your user can either inherit customer or seller or both
    
    class User(Seller):
        pass
    #OR
    
    class User(Buyer):
        pass
    #OR
    class User(Seller, Buyer):
        pass