Search code examples
pythondjangodjango-users

Python/django inherit from 2 classes


In my django project I have 2 variations of users. One subclasses User class from django.auth and second uses almost the same fields but is not a real user (so it doesn't inherit from User). Is there a way to create a FieldUser class (that stores fields only) and for RealUser subclass both FieldUser and User, but for FakeUser subclass only FieldUser ?


Solution

  • sure, I've used multiple inheritance in django models, it works fine.

    sounds like you want to setup an abstract class for FieldUser:

    class FieldUser(models.Model):
        field1 = models.IntegerField()
        field2 = models.CharField() #etc
        class Meta:
            abstract=True #abstract class does not create a db table
    
    class RealUser(FieldUser, auth.User):
        pass #abstract nature is not inherited, will create its own table to go with the user table
    
    class FakeUser(FieldUser):
        pass #again, will create its own table