4

I have this model:

class MyModel(User):
    #others fields

and this serializer:

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ('username', 'password', 'some_field')

I get data from ajax to make a login and I handle it like this:

serializer = MySerializer(data=request.DATA)
print(serializer.is_valid())

The problem: When I send any data my serializer works but when my username field, which must be unique as User model describe, matches with one at the database the serialize become invalid, so serializer.is_valid() return False

Why? can not I create a serialize object with data which must be unique and already exists in the database?

Andres
  • 4,323
  • 7
  • 39
  • 53
  • its trying to create a new model instance of `MyModel`, but it cant do so validly because the username already exists. You should be using [AbstractUser](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser) instead to subclass user. – agconti Jul 24 '14 at 20:13
  • you should also make sure the lookup field on your serializer is set to `username`. – agconti Jul 24 '14 at 20:14

1 Answers1

3

Because you are using ModelSerializer which automatically generates validators for your serializer. You should use normal Serializer class instead.

Validation in REST framework

Abdullah Alharbi
  • 311
  • 1
  • 5
  • 9