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

Patching a field on a model with a ManyToManyField removes the related objects #2829

Closed
pyeekwr opened this issue Apr 16, 2015 · 6 comments
Closed
Labels
Milestone

Comments

@pyeekwr
Copy link

pyeekwr commented Apr 16, 2015

I am currently on drf 3.0

I am running into an issue where when I am trying to PATCH a field on a model with a ManyToManyField, the model serializer produces a deserialization where the related many to many relation gets removed, even though the field payload does not include the many to many field. Is this intended behavior? or am I configuring this incorrectly?

Test case

@tomchristie
Copy link
Member

Sorry to not be more help, but the issue needs to be more clearly outlined as it currently stands.
What's the most minimal possible test case you can put together for this?
Is this the same description as #2761?

@pyeekwr
Copy link
Author

pyeekwr commented Apr 17, 2015

This is actually the minimal test case that I can come up with, I tried testing the serializer itself with the model but couldn't reproduce it. It only happens when I have a updating view + serializer + model that I see this happen. I'm new to django so I'm not even sure where to decouple everything to debug this, since all I'm really doing is configuration (declaratively specifying the view/serializer/model). so I wasn't sure if I was configuring things correctly

@pyeekwr
Copy link
Author

pyeekwr commented Apr 17, 2015

This is the test I ran against the serializer itself

class TestSerializerForModelWithM2MField(TestCase):
    def test_patch_many_to_many(self):
        new_author_data = { 
            'name': 'pyeekwr'
            }   
        test_author = Author.objects.create(**new_author_data)

        new_stuff_data = { 
            'content': 'This is my content body',
            'description': 'This is the description',
            }   

        test_stuff = Stuff.objects.create(**new_stuff_data)
        test_stuff.authors.add(test_author)

        patch_data = { 
            'description': 'This is the new description',
            }   

        serializer = StuffSerializer(test_stuff, data=patch_data, partial=True)
        serializer.is_valid()
        instance = serializer.save()
        self.assertEqual(instance.description, patch_data['description'])
        self.assertEqual(instance.authors.count(), 1)

@pyeekwr pyeekwr closed this as completed Apr 17, 2015
@pyeekwr pyeekwr reopened this Apr 17, 2015
@pyeekwr
Copy link
Author

pyeekwr commented Apr 21, 2015

any suggestions on what test case I can build? I can do that if you''re able to tell me.

@tomchristie
Copy link
Member

This looks valid to me. Confirmation of reproducibility from anyone else would help us figure out if we should be prioritizing it, and if it's still valid.

@xordoquy xordoquy modified the milestone: 3.1.4 Release Jul 8, 2015
@xordoquy xordoquy modified the milestones: 3.1.4 Release, 3.1.5 Release Jul 16, 2015
@tomchristie tomchristie modified the milestones: 3.1.5 Release, 3.2.1 Release Jul 30, 2015
@tomchristie tomchristie modified the milestone: 3.2.1 Release Aug 7, 2015
@tomchristie tomchristie added this to the 3.4.5 Release milestone Aug 10, 2016
@tomchristie
Copy link
Member

tomchristie commented Aug 10, 2016

Assuming this is closed along with #2761

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

No branches or pull requests

3 participants