Dajgo Validation error

36 views
Skip to first unread message

Soumen Khatua

unread,
Jan 8, 2020, 11:07:50 AM1/8/20
to django...@googlegroups.com
Hi Folk,

What is the process to validate fields in django rest framework?
like email and phone number should be unique and password should be alphanumeric character only.

Where i need to write the logic inside serializers or views part and how i can do that?

Thank you in advance

Regards,
Soumen


Suraj Thapa FC

unread,
Jan 8, 2020, 11:15:30 AM1/8/20
to django...@googlegroups.com
For email you can set email field in model as unique=True and the password should be  stored in hash.. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6Wb7_45-r165wg10M-w%2B6V6zOheCnEC3_chbJ-GEHRGQLA%40mail.gmail.com.

Soumen Khatua

unread,
Jan 8, 2020, 12:07:05 PM1/8/20
to django...@googlegroups.com

seriazlizers.py

    email = serializers.EmailField(required=True,validators=[UniqueValidator(queryset=User.objects.all())])
    phone_number = serializers.CharField(required = True,validators=[UniqueValidator(queryset=User.objects.all())])
    password = serializers.CharField(write_only=True,required=True,
                                        help_text='Leave empty if no change needed',
                                        style={'input_type': 'password', 'placeholder': 'Password'}
                                    ) 

models.py
    email = models.EmailField(_('email address'), unique=True)
    phone_number = models.CharField(max_length=10,unique = True,
                                    blank=False,default = uuid.uuid4().hex[:8])
    first_name = models.CharField(_('first name'), max_length=30)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_active = models.BooleanField(_('active'), default=True)
    is_staff = models.BooleanField(_('staff status'),default=False)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)


  I already did that but I want to raise validation error that email should be unique and password should be alphanumeric character in json format?

Suraj Thapa FC

unread,
Jan 9, 2020, 7:59:53 AM1/9/20
to django...@googlegroups.com
Use try catch and send  whatever response you want to send

Reply all
Reply to author
Forward
0 new messages