django-tastypie: Posting to a Resource having ManytoMany field with through relationship

后端 未结 3 1530
既然无缘
既然无缘 2021-01-05 20:10

I\'m working on a API for a project and I have a relationship Order/Products through OrderProducts like this:

In models.py

class Product(models.Model         


        
3条回答
  •  遥遥无期
    2021-01-05 20:56

    Since you needed the manytomany field only for listing, a better solution is to add readonly=True on OrderResource's products field. This removes the need of overriding save_m2m method. For completeness:

    class OrderResource(ModelResource):
        products = fields.ToManyField('order.api.ProductResource', products, 
                                      readonly=True, full=True)
    
        class Meta:
            queryset = Order.objects.all()
            resource_name = 'order'
    

提交回复
热议问题