django-restql icon indicating copy to clipboard operation
django-restql copied to clipboard

Error with nested validate

Open alepn opened this issue 4 years ago • 7 comments
trafficstars

When I use validate data method on a nested serializer, I receive the following message:

"create is not a valid operation, valid operations for this request are add, create, remove, update"

alepn avatar May 13 '21 19:05 alepn

Can you show us the way you are using validate data method on your nested serializer so that we at least get a starting point, posting a piece of code which you think is causing that error would be very helpful to anyone who wants to help you. It’s better to mention the version of django-restql which you are using to make things easier.

yezyilomo avatar May 13 '21 19:05 yezyilomo

MODELS

class Profile(models.Model):
    name = models.CharField('Name', max_length=80)

class Email(models.Model):
    profile = models.ForeignKey(Profile)
    email = models.CharField('Email', max_length=80, unique=True)

SERIALIZERS

from django_restql.mixins import DynamicFieldsMixin
from django_restql.serializers import NestedModelSerializer

class EmailSerializer(DynamicFieldsMixin, NestedModelSerializer):
    
    def validate(self, data):
        if data['email']:
            q = Email.objects.filter(email=data['email'])
            if self.instance:
                q = q.exclude(pk=self.instance.id)
            if q.exists():
                raise serializers.ValidationError("Email already exists.")
        return data
    
    class Meta:
        model = Email
        fields = ['profile','email']

class ProfileSerializer(DynamicFieldsMixin, NestedModelSerializer):
    email_set = NestedField(EmailSerializer, many=True, required=False, allow_null=True)
    class Meta:
        model = Email
        fields = ['name','email_set']

Tested with django-restql versions: 0.10.2 and 0.11.2

alepn avatar May 14 '21 12:05 alepn

There is nothing wrong with your code, I think the problem is on the way you are sending your request, you are probably adding space(s) before or after the word create.

NOTE: "create " != "create" != " create" != " create " Make sure you are not leaving spaces before or after the the operation "create"

yezyilomo avatar May 14 '21 17:05 yezyilomo

The request body is right. This message appears only when I use the validate. When validate is removed, the request works normaly.

alepn avatar May 14 '21 18:05 alepn

I think there's something wrong in your code, I have tried to reproduce your scenario, to me it works just fine you don't even need the validate method the DRF handles it for you, but even if you use it everything works fine. if I create email it creates successfully, if I try to re-create the same email it tells me that it already exists.

yezyilomo avatar May 15 '21 15:05 yezyilomo

Yes, it works until the step you've tested. But if you try to re-send the request, with a new valid email, the DRF shows a fk constraint error on nested item. I think the fk id on nested item is lost when DRF raises a exception.

alepn avatar May 17 '21 15:05 alepn

I tried to use the validate on serializer, but I noticed that the DRF constraint validation always is throwed before the validate method on serializer.

alepn avatar May 17 '21 15:05 alepn