简体   繁体   English

Django 模型保存 () - AttributeError: 'NoneType' 对象没有属性 'append'

[英]Django model save() - AttributeError: 'NoneType' object has no attribute 'append'

In my models.py I want to extend the Django User model by adding an email_list.在我的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)
    [...]

This email_list has to have the user email as default value.此 email_list 必须将用户电子邮件作为默认值。 I found that the best way to do this, is by overriding the save() method:我发现最好的方法是重写save()方法:

def save(self, *args, **kwargs):
    self.email_list.append(self.email)
    super(User, self).save(*args, **kwargs)

However, when I add a user I get the following error:但是,当我添加用户时,出现以下错误:

AttributeError: 'NoneType' object has no attribute 'append'

And the print(type(self.email_list)) , returns <type 'NoneType'>print(type(self.email_list))返回<type 'NoneType'>

What is wrong with the ArrayField ? ArrayField有什么问题?

You should use a callabe such as list for the default value.您应该使用诸如列表之类的 callabe 作为默认值。

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/ https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/fields/

the initial None value is occurring because null=True, even if default=list.出现初始 None 值是因为 null=True,即使 default=list。 to avoid default value overriding, remove or set null to False为避免覆盖默认值,删除或将 null 设置为 False

from django.contrib.postgres.fields import ArrayField

class User(AbstractUser):
    email_list = ArrayField(models.EmailField(max_length=100), blank=True, default=list)
[...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'NoneType' object 在 Django 项目中没有属性 'append' 错误 - AttributeError: 'NoneType' object has no attribute 'append' error in Django project AttributeError: 'NoneType' object 没有属性 'save' python-django - AttributeError: 'NoneType' object has no attribute 'save' python-django AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - AttributeError: 'NoneType' object has no attribute 'append' 错误“ AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39;” - Error “AttributeError: 'NoneType' object has no attribute 'append'” Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - Python : AttributeError: 'NoneType' object has no attribute 'append' AttributeError:&#39;NoneType&#39;对象没有属性&#39;save&#39; - AttributeError: 'NoneType' object has no attribute 'save' Django:AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - Django: AttributeError: 'NoneType' object has no attribute 'split' Django - AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;pk&#39; - Django - AttributeError: 'NoneType' object has no attribute 'pk' Django - AttributeError:“NoneType”对象没有属性“方法” - Django - AttributeError: 'NoneType' object has no attribute 'method' 将列表附加到列表:AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - Append a list to a list: AttributeError: 'NoneType' object has no attribute 'append'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM