更新了继承的django模型字段属性?

时间:2013-03-08 14:20:16

标签: python django django-models

说我继承了django用户类:

# import AbstractUser class ....
class MyUser(AbstractUser):

    gender = models.Charfield(....
    ....
    # I want to make the email field blank=False and unique=True
    # however, to my knowledge, python doesn't allow you to 
    # override inherited fields

那么如何使电子邮件字段为空= False且unique = True。注意:我也使用South,所以South友好的代码会很好。 (注意:我正在使用Django 1.5,所以希望有一个解决方案,所以我不必从AbstractBaseUser创建自己的用户模型)

1 个答案:

答案 0 :(得分:2)

class MyUser(AbstractUser):

    gender = models.Charfield(....

    def __init__(self, *args, **kwargs):
       super(self, MyUser).__init__(*args, **kwargs)
       email = models.EmailField(blank=False, unique=True)
       email.contribute_to_class('email', self)