0

I have a set up a route for a RetrieveUpdateAPIView and am testing it in Postman using PATCH.

I am writing a value into a nested field, which is working but deletes all other instances of that nested model.

Here is what I enter in 'JSON body' in Postman

{
    "cards": [
        9
    ]
}

When I PATCH the value '9', it deletes all other 'card' instances. I was expecting it to simply add that 'card' instance without taking the others away.

This is an example of the response:

{
    "id": 19,
    "name": "collector two",
    "email": "two@example.com",
    "cards": [
        9
    ]
}

Here is my view code

class CollectorRetrieveView(generics.RetrieveUpdateAPIView):
    queryset = Collector.objects.all()
    serializer_class = SmallCollectorSerializer
    permission_classes = [HasAPIKey | IsAuthenticated]
    lookup_field = 'email'

Serializer

class SmallCollectorSerializer(serializers.ModelSerializer):

    class Meta:
        model = Collector
        fields = ["id", "name", "email", "cards"]

Route

path("collector_retrieve/<email>", CollectorRetrieveView.as_view(), name="collector_retrieve"),

I tried to PATCH 'card' 9 without deleting the other card instances

models

class Collector(models.Model):
    ...
    name = models.CharField(max_length=128, null=True)
    email = models.EmailField(null=True)
    slug = models.CharField(unique=True, max_length=100, null=True, blank=True)
    ...   

    def __str__(self):
        return self.name


class Card(models.Model):
    title = models.CharField(max_length=128)
    desc = models.CharField(max_length=1024)
    ...
    collector = models.ManyToManyField(Collector, related_name="cards")
    ...
  • Share the `Collector` model. I assume that cards are ManyToManyField. Did you read [Dealing with nested objects](https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects) and [Writable nested serializers](https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers). – NKSM Jul 25 '23 at 12:06
  • [This thread](https://stackoverflow.com/questions/61537923/update-manytomany-relationship-in-django-rest-framework) may help you. – NKSM Jul 25 '23 at 12:08
  • Good point, I have shared the models. Thank you! – Christopher Jul 25 '23 at 13:01

0 Answers0