在我的 models.py我想通过添加 email_list 来扩展 Django 用户模型。
from django.contrib.postgres.fields import ArrayField
class User(AbstractUser):
email_list = ArrayField(models.EmailField(max_length=100), null=True, blank=True)
[...]
此 email_list 必须将用户电子邮件作为默认值。我发现最好的方法是覆盖 save()方法:
def save(self, *args, **kwargs):
self.email_list.append(self.email)
super(User, self).save(*args, **kwargs)
但是,当我添加用户时出现以下错误:
AttributeError: 'NoneType' object has no attribute 'append'
还有 print(type(self.email_list)) , 返回 <type 'NoneType'>
ArrayField 出了什么问题? ?
请您参考如下方法:
您应该使用诸如列表之类的调用作为默认值。
from django.contrib.postgres.fields import ArrayField
class User(AbstractUser):
email_list = ArrayField(models.EmailField(max_length=100), null=True, blank=True, default=list)
[...]
https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/fields/
