How to select data from filtered Data to send POST request using ModelViewSet in DRF?

I have a model.

class MyClass(models.Model):
    field1 = models.ForeignKey("Class1", on_delete=models.PROTECT)
    field2 = models.ForeignKey("Class2", on_delete=models.CASCADE)
    field3 = models.ForeignKey("Class3", on_delete=models.PROTECT)

This is my ViewSet:

class MyViewSet(ModelViewSet):
    serializer_class = serializers.MySerializer

    def create(self, request, *args, **kwargs): 
        value1 = Class1.objects.filter(id__in={x.id for x in self.request.user.organization_modules})
        value2 =  Class2.objects.filter(organization=self.request.user.organization)
        value3 = Class3.objects.get(id=self.kwargs["pk"])
        obj = MyClass(field1=value1, field2=value2, field3=value3) 
        obj.save() 
        return Response(status=status.HTTP_201_CREATED)  

This is the URL:

path("api/", MyViewSet.as_view({"post":"create"}))

In the above example,I am overriding create method of ModelViewSet and performing the data selection to save the data. But when I hit the API, I want to select values of field1, field2 and field3 from filtered data, not from all the data of Class1, Class2 and Class3. But in current implementation, all the data of Class1, Class2 and Class3 is shown.

How to do that?

Back to Top