在我的models.py
中,我想通过添加email_list扩展Django User模型。
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/