Python / django继承自2个类

时间:2010-06-23 18:33:20

标签: python django django-users

在我的django项目中,我有两种不同的用户。来自django.auth的一个子类User类和第二个使用几乎相同的字段但不是真实用户(因此它不从User继承)。有没有办法创建一个FieldUser类(只存储字段)和RealUser子类的FieldUser和User,但是对于FakeUser子类只有FieldUser?

1 个答案:

答案 0 :(得分:4)

当然,我在django模型中使用了多重继承,它运行正常。

听起来你想为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
相关问题