Search code examples
pythondjangodjango-rest-frameworkmanytomanyfield

ManytoMany related to django Rest


modes.py

class Product(models.Model):
    product_name = models.CharField(max_length=32)
    quantity = models.IntegerField()
    remarks = models.TextField(blank=True)

class Vendor(models.Model):
    vendor_name = models.CharField(max_length=50)
    address = models.CharField(max_length=100)
    bill_no = models.CharField(max_length=8)
    product = models.ManyToManyField(Product)

serializers.py

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

class VendorSerializer(serializers.ModelSerializer):
    product = ProductSerializer(many=True, read_only=False)
    class Meta:
        model = Vendor
        fields = '__all__'
    def create(self, validate_data):
        product_data = validate_data.pop('product')
        vendor = Vendor.objects.create(**validate_data)
        for product_data in product_data:
            Product.objects.create(vendor=vendor, **product_data)
        return Vendor

views.py

class VendorViewset(viewsets.ModelViewset):
      serializer_class = VendorSerializer
      queryset = Vendor.objects.all()

How should I write product view such that It can be demonstrated that products of certain vendor can only be viewed with url routing?


Solution

  • you can use the @detail_route:

    from rest_framework.decorators import detail_route
    
    class VendorViewset(viewsets.ModelViewset):
          serializer_class = VendorSerializer
          queryset = Vendor.objects.all()
    
        @detail_route(methods=['GET'])
        def products(request, pk=None):
            qs = self.get_object().product.all()
            serializer = ProductSerializer(qs, many=True)
            return Response(serializer.data)
    

    and then the vendor products will be available by

    YOUCURRENT_PATH_TO_DETAIL_VENDOR/products