def validate(self, attrs):
"""
Assert that filled_out can't be turned off and that agreed_to_terms_of_service is true
"""
if 'filled_out' in attrs and not attrs['filled_out']:
raise ValidationError("filled_out cannot be set to false")
if 'agreed_to_terms_of_service' in attrs and not attrs['agreed_to_terms_of_service']:
raise ValidationError("agreed_to_terms_of_service cannot be set to false")
# Postal code is only required in United States and Canada
country = attrs.get("country", "")
postal_code = attrs.get("postal_code", "")
if country in ("US", "CA") and not postal_code:
raise ValidationError("postal_code may not be blank")
return super(ProfileFilledOutSerializer, self).validate(attrs)
评论列表
文章目录