0

I'm building a Django REST API which has access to our existing database with existing users. The purpose of this API is allowing the upcoming mobile application to make requests.

I'm sending a post request to a view with custom authenticator to verify the sent account details.

My existing model:

class LogonAccount(models.Model):
    id = models.BigIntegerField(primary_key=True)
    email = models.TextField()
    two_step_enabled = models.BooleanField()
    password = models.TextField(blank=True, null=True)
    username = models.TextField(unique=True, blank=True, null=True)

My View and Serializer

class LogonAccountViewSet(viewsets.ModelViewSet):
    queryset = LogonAccount.objects.all().order_by('username')
    serializer_class = LogonAccountSerializer

class LogonAccountSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = LogonAccount
        fields = ('username', 'email')

Sending a post request to this endpoint with username and password in its body keeps returning me a bad request (400) with:

{
    "username": [
        "logon account with this username already exists."
    ],
    "email": [
        "This field is required."
    ]
}

Making the fields not required in the serializer just changes the error to database constraints (null value in column id)

class LogonAccountSerializer(serializers.HyperlinkedModelSerializer):
    username = serializers.CharField(required=False)
    email = serializers.CharField(required=False)

    class Meta:
        model = LogonAccount
        fields = ('username', 'email')

I'm not trying to insert data, just trying to validate it. What am I doing wrong or how do I stop it from trying to insert data?

1 Answer 1

0

The error you are getting is not because the DRF is trying to insert data but because of the validations available in the serializer, you are using. You are getting two validation errors:

  1. email is required. Since in your model, the email field is not allowing blank or null values, so in the serializer, this is treated as a required field. So you need to handle it. Either you send that in the post request or make that field non-mandatory.

  2. username is violating a unique constraint. In your model, you have set the username field as unique. So when a serializer is generated this unique field validation is added to that field.

If you want to see the serializer generated for your model serializer and all the fields and validations for the serializer, you can use the following code:

ser = LogonAccountSerializer()
print(repr(ser))

After the comment from @spoontech. The DB operation is performed because you are using viewsets.ModelViewSet. If you just want to use the serializer for validating data, you can use it this way:

serializer = LogonAccountSerializer(request.data)
is_valid = serializer.is_valid()
if is_valid:
   # do something using serializer.validated_data
else:
   # access errors using serializer.errors()
Sign up to request clarification or add additional context in comments.

1 Comment

However when making both fields non-required. I'm getting this error: django.db.utils.IntegrityError: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, , , null, null, null, null, cassandra2, null). Which sounds like its trying to insert data

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.