Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with nested validate #214

Open
alepn opened this issue May 13, 2021 · 7 comments
Open

Error with nested validate #214

alepn opened this issue May 13, 2021 · 7 comments

Comments

@alepn
Copy link

alepn commented May 13, 2021

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"

@yezyilomo
Copy link
Owner

yezyilomo commented May 13, 2021

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.

@alepn
Copy link
Author

alepn commented May 14, 2021

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

@yezyilomo
Copy link
Owner

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"

@alepn
Copy link
Author

alepn commented May 14, 2021

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

@yezyilomo
Copy link
Owner

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.

@alepn
Copy link
Author

alepn commented May 17, 2021

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
Copy link
Author

alepn commented May 17, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants