Partial update and cross-field validation

22 views
Skip to first unread message

Juan Antonio Alvarez

unread,
Dec 1, 2016, 9:13:31 AM12/1/16
to Django REST framework
Hi there!

I'm working on an application that needs to do lots of cross checking. We also support partial updates, son on every save I usually end up doing something like:

if 'validate_field' in self.validated_data:
  validation_value = self.validated_data['validate_field']
else:
  validation_value = self.instance.validate_field
# .. Validation logic here

I keep asking myself:
* Is this a valid approach? As in, will it bite me later?
* Is there a better way to do it?

Thank you,

Juan

Tom Christie

unread,
Dec 1, 2016, 10:17:13 AM12/1/16
to Django REST framework
That seems perfectly reasonable to me, yup, although you might want to use the following pattern:

    field_value = self.validated_data.get('validate_
field', self.instance.validated_field)

Juan Antonio Alvarez

unread,
Dec 1, 2016, 10:37:07 AM12/1/16
to Django REST framework
That seems perfectly reasonable to me, yup, although you might want to use the following pattern:

    field_value = self.validated_data.get('validate_
field', self.instance.validated_field)

More pythonic indeed. Thank you! 
Reply all
Reply to author
Forward
0 new messages