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

Remove UniqueValidator from nested serializers on create method #46

Closed
Nekmo opened this issue Jul 2, 2018 · 6 comments
Closed

Remove UniqueValidator from nested serializers on create method #46

Nekmo opened this issue Jul 2, 2018 · 6 comments

Comments

@Nekmo
Copy link

Nekmo commented Jul 2, 2018

Hello, first of all thank you for creating this library. I think it should be included in the core!

I'm having a problem with the nested user serializer. Validation prevents set an existing user on POST method:

HTTP 400 Bad Request
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
  
{
    "user": {
        "username": [
            "A user with that username already exists."
        ]
    }
}

My code:

class UserSerializer(HispaSerializerMixin, NestedCreateMixin, NestedUpdateMixin,
                     serializers.HyperlinkedModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ('url', 'id', 'username', 'first_name', 'last_name', 'is_staff', 'is_active')

class GuardianUserPermSerializer(HispaSerializerMixin,
                                 NestedCreateMixin, NestedUpdateMixin,
                                 serializers.HyperlinkedModelSerializer):
    user = UserSerializer()

    class Meta:
        model = UserObjectPermission
        exclude = ()

I have investigated the matter a bit and have found a solution:
https://medium.com/django-rest-framework/dealing-with-unique-constraints-in-nested-serializers-dade33b831d9

However, drop validation is a very dirty solution. The validation should be removed only if the user already exists.

Thank you.

@ruscoder
Copy link
Member

ruscoder commented Jul 4, 2018

The similar issue is #1

@ruscoder
Copy link
Member

ruscoder commented Jul 4, 2018

@Nekmo Hello!
DRF describes this case here:
http://www.django-rest-framework.org/api-guide/validators/#updating-nested-serializers

@Nekmo
Copy link
Author

Nekmo commented Jul 7, 2018

Thanks for your quick response! If this report is duplicated then it can be closed.

Would not it be possible to automatically remove the validation if the instance already exists and the unique fields are not edited?

Thank you.

@ruscoder
Copy link
Member

@Nekmo
We've implemented a workaround for this case:

https://github.com/beda-software/drf-writable-nested#validation-problem-for-nested-serializers-with-unique-fields-on-update

@Nekmo
Copy link
Author

Nekmo commented Aug 23, 2018

@ruscoder Thank you!

@Nekmo Nekmo closed this as completed Aug 23, 2018
@ruscoder
Copy link
Member

@Nekmo Don't forget to wrap save() in the first serializer into transaction.atomic to avoid partial objects creating when a validation error happens.

I'm not sure about implementing this wrapping inside the library. I'll create an issue about atomic transactions to collect feedback.

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