I am trying to serialize an email field using Django Rest Framework, however the server is saying not accepting any email. All emails are greeted with error "Enter a valid email address". Below are my configurations.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'password')
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, verbose_name='email address')
first_name = models.CharField(max_length=256, verbose_name="First Name", blank=True)
last_name = models.CharField(max_length=256, verbose_name="Last Name", blank=True)
id = models.UUIDField(primary_key=True, unique=True)
data = JSONField(default=default_data, name="device_data")
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
@deconstructible
class EmailValidator:
message = _('Enter a valid email address. Not valid')
code = 'invalid'
...
domain_whitelist = ['localhost', 'gmail', 'gmail.com', '@gmail.com']
http -f POST http://127.0.0.1:8000/reg email="l55@gmail.com", password='pw'
HTTP/1.1 400 Bad Request
Allow: POST, OPTIONS
Content-Length: 42
Content-Type: application/json
Date: Mon, 30 Mar 2020 00:11:14 GMT
Server: WSGIServer/0.2 CPython/3.7.3
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"email": [
"Enter a valid email address."
]
}
Can you help me?
Thank you!
In your httpie
call you have a comma after the email address, this is being appended to the email. You need to remove it, l55@gmail.com,
is not a valid email address
http -f POST http://127.0.0.1:8000/reg email="l55@gmail.com" password='pw'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.