Django REST Framework: Unique_together validation on Serializers -
having issues serializer.is_valid()
returning true
if serializer instance fails unique_together
constraint on model side.
is there way me specify in serializer enforce unique_together
constraint?
unfortunately, andreas's answer not quite complete, not work in case of update.
instead, want more like:
def validate(self, attrs): field1 = attrs.get('field1', self.object.field1) field2 = attrs.get('field2', self.object.field2) try: obj = model.objects.get(field1=field1, field2=field2) except statewithholdingform.doesnotexist: return attrs if self.object , obj.id == self.object.id: return attrs else: raise serializers.validationerror('field1 field2 exists')
this work put, patch, , post.
Comments
Post a Comment