0

I have a nested serializer which binds some fields that are foreign keys to the related objects, when i make a get request I get the data as I want it, but when I make a patch request I would like to be able to only provide the foreign key id instead of the whole object representation.

EDIT:

class Preferences(models.Model):
    user = models.ForeignKey(User, related_name='preferences', on_delete=models.CASCADE)
    current_patient = models.ForeignKey(Patient, null=True, on_delete=models.CASCADE, default=None)
    current_professional = models.ForeignKey(Professional, null=True, on_delete=models.CASCADE, default=None)
    current_entity = models.ForeignKey(Entity, null=True, on_delete=models.CASCADE, default=None)
    current_supervisor = models.ForeignKey(Supervisor, null=True, on_delete=models.CASCADE, default=None)



class PreferencesSerializer(serializers.ModelSerializer):
    current_patient = PatientSerializer(many=False)
    current_professional = ProfessionalSerializer(many=False)
    current_entity = EntitySerializer(many=False)
    current_supervisor = SupervisorSerializer(many=False)

    class Meta:
        model = Preferences
        fields = '__all__'


class PreferencesView(generics.RetrieveUpdateAPIView):
    serializer_class = PreferencesSerializer


    def get_object(self):
        return Preferences.objects.get(user=self.request.user)
  • Use a different serializer for a viewset that implements patching. – Willem Van Onsem Jul 30 '21 at 18:15
  • But I want to be able to get a response that includes the data as my current serializer does. – Luis Alfonso Buelvas Betancour Jul 30 '21 at 18:18
  • but I don't see the issue: you make two sorts of serializers, one with the nested data, to retrieve, and one with the primary keys to put/patch/... – Willem Van Onsem Jul 30 '21 at 18:20
  • Yes, but I would need to write two views, or a complicated way to select the serializers, and yet I guess the response from the patch method wont be the same as the response from the get method. – Luis Alfonso Buelvas Betancour Jul 30 '21 at 18:23
  • Can you please share an example of your code? It would be much easier to help you if you do so :) Also, please share the request you are making. – gripep Jul 30 '21 at 18:48
  • @LuisAlfonsoBuelvasBetancour You can use `get_serializer_class` and use another serializer for patch requests in the same viewset. You can also override the `update` method in your serializer if you're using `ModelSerializer` or have inherited from the right mixins. The point is, There are several ways to achieve what you want, you should include some codes in your question to get more detailed answer about the better approach. – Sajad Jul 30 '21 at 18:50
  • Just updated my question, as you can see the nested fields are all foreign keys, writing two serializers "works", but I want the same response format from a nested serializer. – Luis Alfonso Buelvas Betancour Jul 30 '21 at 19:04
  • [This](https://stackoverflow.com/a/29953188/6759844) shows an interesting way to solve this – Brian Destura Jul 31 '21 at 06:58

1 Answers1

0

You can override your view's get_serializer_class method:

class PreferencesView(generics.RetrieveUpdateAPIView):
    def get_serializer_class(self):
        if self.request.method == "PATCH":
            return PatchPreferencesSerializer

        return PreferencesSerializer
        
    # ...

Please refer to Django REST documentation: https://www.django-rest-framework.org/api-guide/generic-views/#attributes

gripep
  • 379
  • 3
  • 13