django rest framework validation & refreshing data -


i trying validate whether password1, password2 matching not using object level validation because executed after field level validations, validate_field accepts 1 value. how can implement following in rest framework?

def validate_password2(self, data):         print 'validating...'         password1, password2 = data['password1'], data['password2']                    if password1 , password2 , password1 != password2:                raise serializers.validationerror('the 2 passwords not match.')                  return password2 

and when errors occur, data in form cleared. how can retain input data when errors occur django form.fields?

instead of adding field-level validation, add check object-level validation because require access multiple fields.

even drf docs define this:

object-level validation

to other validation requires access multiple fields, add method called .validate() serializer subclass. method takes single argument, dictionary of field values. should raise validationerror if necessary, or return validated values.

you can following:

def validate(self, data):     ...     password1 = data['password1']     password2 = data['password2']     if password1 , password2 , password1 != password2:         raise serializers.validationerror('the 2 passwords not match.')     ....     return data 

also access initial data when errors have occured, can use your_serializer.initial_data.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -