Python/Django 继承自两个类
在我的Django项目中,我有两种不同的用户类型。一个是从Django的User类派生出来的,是真正的用户;另一个虽然有差不多的字段,但并不是真正的用户(所以它没有继承User类)。有没有办法创建一个FieldUser类(只存储字段),然后让RealUser类同时继承FieldUser和User,而FakeUser类只继承FieldUser呢?
1 个回答
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