Django Rest Framework model serializer with out unique together validation

前端 未结 2 731
粉色の甜心
粉色の甜心 2021-01-04 02:01

I have a model with some fields and a unique together:

....
class Meta(object):
    unique_together = (\'device_identifier\', \'device_platform\         


        
相关标签:
2条回答
  • 2021-01-04 02:47

    You need to remove the validator from the serializer's list.

    Although not exactly the same, the steps are explained here

    0 讨论(0)
  • 2021-01-04 02:49

    Django REST framework applies the UniqueTogetherValidator on the serializer. You can remove this by override the validators field in the serializer definition.

    class ExampleSerializer(serializers.ModelSerializer):
        class Meta:
            validators = []
    

    Note that this also removes the other unique-check validators that are applied on the model, which might not be the best idea. To avoid that, just override the get_unique_together_validators method on the serializer, to ensure only unique-together check is removed.

    class ExampleSerializer(serializers.ModelSerializer):
        def get_unique_together_validators(self):
            """Overriding method to disable unique together checks"""
            return []
    
    0 讨论(0)
提交回复
热议问题