10

I have the following model:

class UserProfile(models.Model):
    mobileNumber = models.BigIntegerField(primary_key=True)
    authKey = models.CharField(max_length=300,null=False,blank=False)
    creationDateTime = models.DateTimeField(auto_now_add=True)
    lastUpdateDateTime = models.DateTimeField(auto_now=True)

Serializer:

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('mobileNumber','authKey')

If userprofile model already has a mobilenumber XX44 and if I try to serialize using UserProfileSerializer with json {'mobileNumber': XX44, 'authKey': u'ggsdsagldaslhdkjashdjkashdjkahsdkjah'} I'm getting the following error:

{'mobileNumber': [u'User profile with this MobileNumber already exists.']}

because model validations are being run for the serializer field.

How can I stop execution of model field validation for mobileNumber. I have tried validate and validate_mobileNumber methods in serializer but they still are executing the model validations.

2
  • The data you are trying to serialize {'mobileNumber':XX44.....} Is it some random data or data of UserProfile model? Because if it is random unvalidated data then you should not use model serializer to serialize it, just use a normal serializer. Commented Mar 25, 2014 at 18:25
  • Try removing 'primary_key=True' from the UserProfile Model. Commented Aug 26, 2015 at 11:30

3 Answers 3

2

remove unique constraint on mobile number of table,so django serializer will validate according to that .

or alternatively,

   serializer=UserProfileSerializer(data=request.DATA,partial=True)
Sign up to request clarification or add additional context in comments.

Comments

0

I understand you won't save the serializer data. So, you can set mobileNumber as a read_only field on UserProfileSerializer.

Check serializer fields documentation for more info: http://www.django-rest-framework.org/api-guide/fields/#core-arguments

Comments

0

By overriding the model field within the serializer, and specifying required=False, allow_blank=True, allow_null=True:

class SomeModel(models.Model):
   some_model_field_which_is_required = models.ForeignKey(...)
   some_other_required_field = models.CharField(...)

class SomeModelSerializer(serializers.ModelSerializer):
    some_model_field_which_is_required = SomeNestedSerializer(
        many=True, required=False, allow_blank=True
    )
    some_other_required_field = serializers.CharField(required=False, allow_blank=True)

   def validate(self, *args, **kwargs):
       print('should get here')

   def validate_some_other_required_field(self, *args, **kwargs):
       print('should also get here')

    class Meta:
        model = SomeModel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.