需要电子邮件字段 django.contrib.auth
Email field required django.contrib.auth
如果提供了所有用户名、密码和电子邮件字段,我想在 django-rest-auth 中注册一个用户。 (我也想实现令牌认证以获得来自服务器的 JSON 响应。)
django.contrib.auth.User
中的默认电子邮件字段是可选的。但我想设置在数据库中注册所需的电子邮件归档,以便用户在没有电子邮件的情况下发出 POST 请求时得到 HTTP 错误响应。
在项目中我通过以下代码注册新用户
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', User.USERNAME_FIELD, "password", 'full_name',
'is_active', 'links', )
write_only_fields = ('password',)
read_only_fields = ('id',)
def create(self, validated_data):
print("Validated Data",validated_data)
if 'email' in validated_data:
user = get_user_model().objects.create(
username=validated_data['username']
)
user.set_email(validated_data['email'])
user.set_password(validated_data['password'])
user.save()
email = EmailMessage('Hello', 'World',
to=[validated_data['email']])
email.send()
user.is_active=False
return user
else:
return None
然而,以上给出:
create() did not return an object instance
如何将电子邮件字段设置为必填字段?
I want to register an user in django-rest-auth, if all username, password and email fields are provided.
在 Django REST 框架中要求序列化器上的字段的正确方法是 set required=True
when initializing the field, or using the extra_kwargs
parameter 将其设置在自动生成的字段上。
In default email field in django.contrib.auth.User is optional. But I want to set the email filed as required to register in the database so that the user gets HTTP error response when POST request has been made without a email.
这意味着默认情况下不需要自动生成的字段,但您仍然可以用 required=True
覆盖它。
However, the above gives:
create() did not return an object instance
这是因为您没有从 create
方法返回 User
实例,就像罐头上写的那样。具体来说,如果不包含 email
字段,您将返回 None
。 None
不是 User
实例,DRF 警告您做错了什么。
如果提供了所有用户名、密码和电子邮件字段,我想在 django-rest-auth 中注册一个用户。 (我也想实现令牌认证以获得来自服务器的 JSON 响应。)
django.contrib.auth.User
中的默认电子邮件字段是可选的。但我想设置在数据库中注册所需的电子邮件归档,以便用户在没有电子邮件的情况下发出 POST 请求时得到 HTTP 错误响应。
在项目中我通过以下代码注册新用户
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', User.USERNAME_FIELD, "password", 'full_name',
'is_active', 'links', )
write_only_fields = ('password',)
read_only_fields = ('id',)
def create(self, validated_data):
print("Validated Data",validated_data)
if 'email' in validated_data:
user = get_user_model().objects.create(
username=validated_data['username']
)
user.set_email(validated_data['email'])
user.set_password(validated_data['password'])
user.save()
email = EmailMessage('Hello', 'World',
to=[validated_data['email']])
email.send()
user.is_active=False
return user
else:
return None
然而,以上给出:
create() did not return an object instance
如何将电子邮件字段设置为必填字段?
I want to register an user in django-rest-auth, if all username, password and email fields are provided.
在 Django REST 框架中要求序列化器上的字段的正确方法是 set required=True
when initializing the field, or using the extra_kwargs
parameter 将其设置在自动生成的字段上。
In default email field in django.contrib.auth.User is optional. But I want to set the email filed as required to register in the database so that the user gets HTTP error response when POST request has been made without a email.
这意味着默认情况下不需要自动生成的字段,但您仍然可以用 required=True
覆盖它。
However, the above gives:
create() did not return an object instance
这是因为您没有从 create
方法返回 User
实例,就像罐头上写的那样。具体来说,如果不包含 email
字段,您将返回 None
。 None
不是 User
实例,DRF 警告您做错了什么。