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

Improve checks for nested creates and updates. #2196

Merged
merged 2 commits into from Dec 3, 2014

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Dec 3, 2014

Closes #2194.

@tomchristie tomchristie added this to the 3.0.1 Release milestone Dec 3, 2014
@kevin-brown
Copy link
Member

kevin-brown commented Dec 3, 2014

LGTM, same fix I was planning on making. 👍

kevin-brown added a commit that referenced this issue Dec 3, 2014
…nested-validation

Improve checks for nested creates and updates.
@kevin-brown kevin-brown merged commit 66bce38 into master Dec 3, 2014
1 check passed
@kevin-brown kevin-brown deleted the tomchristie-improve-update-nested-validation branch Dec 3, 2014
@baylee
Copy link

baylee commented Dec 12, 2014

@tomchristie @kevin-brown thanks for the 3.0.1 release. I just updated and my tests are failing with this new addition (The .create() method does not support writable nestedfields by default.), but as far as I know I'm not attempting to do any nested writes. I have one related Serializer, but it is set to read_only=True and when I post to this endpoint, the related objects all already exist. Any ideas, or am I just doing something wrong?

class Like(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_index=True)
    content_object = generic.GenericForeignKey()

    user = models.ForeignKey(settings.AUTH_USER_MODEL)

    class Meta:
        unique_together = (("user", "content_type", "object_id"),)


class LikeSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True, default=serializers.CurrentUserDefault())

    class Meta:
        model = Like


class LikeViewSet(viewsets.ModelViewSet):
    queryset = Like.objects.all()
    serializer_class = LikeSerializer

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        serializer.save(user=self.request.user)

printed LikeSerializer

LikeSerializer():
    id = IntegerField(label='ID', read_only=True)
    user = UserSerializer(default=CurrentUserDefault(), read_only=True):
        email = EmailField(allow_blank=True, allow_null=True, max_length=75, required=False, validators=[<UniqueValidator(queryset=User.objects.all())>])
        id = IntegerField(label='ID', read_only=True)
        username = CharField(help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=30, validators=[<django.core.validators.RegexValidator object>, <UniqueValidator(queryset=User.objects.all())>])
        fullname = CharField(allow_blank=True, allow_null=True, max_length=80, required=False)
        original_photo = ImageField(allow_null=True, max_length=100, required=False)
        about = CharField(allow_blank=True, allow_null=True, required=False, style={'type': 'textarea'})
        user_following_count = ReadOnlyField()
        user_followers_count = ReadOnlyField()
        content_type = SerializerMethodField()
    created = DateTimeField(read_only=True)
    object_id = IntegerField(required=True)
    content_type = PrimaryKeyRelatedField(queryset=ContentType.objects.all(), required=True)
    class Meta:
        validators = [<UniqueTogetherValidator(queryset=Like.objects.all(), fields=('user', 'content_type', 'object_id'))>]

I'm just posting the object_id and content_type of the object I want to like. Was working fine before I updated.

Thanks!

@xordoquy
Copy link
Collaborator

xordoquy commented Dec 12, 2014

@baylee the support questions are better asked on the mailing list or stack overflow.

@baylee
Copy link

baylee commented Dec 12, 2014

Okay, here it is on SO: http://stackoverflow.com/questions/27452044/drf-3-0-1-create-method-does-not-support-writable-nestedfields

Any explanation or corrections would be appreciated.

@tomchristie
Copy link
Member Author

tomchristie commented Dec 12, 2014

This does actually highlight a possible improvement we could make to that check.

As both 'read_only' and 'default' are being using this isn't actually a nested write, although it does fail our test.

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

Successfully merging this pull request may close these issues.

4 participants