简体   繁体   中英

Django REST framework POST without insert

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM