如何使用多对多关系在Django中反向序列化 [英] How to reverse serialize in django with many to many relations

查看:244
本文介绍了如何使用多对多关系在Django中反向序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先将类别进行分类,然后将产品进行分类,将其与类别

models.py 类别):

  class Category(models.Model):
name = models.CharField(max_length = 191,空白= False,null = False)
描述= models.TextField(blank = True,null = True)

models.py 产品):

  class Product(models.Model):
product_code = models.CharField(max_length = 191,blank = False,null = False)
名称= models.CharField (max_length = 191,blank = False,null = False)
描述= models.TextField(空白= False,null = False)
价格= models.DecimalField(max_digits = 19,decimal_places = 2)
照片= models.ImageField(upload_to ='pictures / products /',max_length = 255,null = False,blank = False)
category = models.name = models.ManyToManyField(Category)

如何获得以下结果:

  {
类别:[
{
id:1,
名称:室内肌肉训练,
描述: null,
产品:{
名称:产品名称,
代码:产品代码
}
},
{
id:2,
名称:室外肌肉训练,
说明:null,
产品:{
名称 : product_name,
code: product_code
}
}
]
}

解决方案

使用 serializer-method 字段。我们的目标是从类别序列化程序中获取产品信息。因此,对于

 类CategorySerializer(serializers.ModelSerializer):
产品= serializers.SerializerMethodField()

类元:
模型=类别
字段=('')#添加相对字段

def get_products(self,obj):
产品= obj.product_set.all()#将返回与此类别关联的产品查询集
response = ProductSerializer(products,many = True).data
return response


I have first made category crud, and then product crud with many-to-many relation with category.
models.py (category):

class Category(models.Model):
    name = models.CharField(max_length=191, blank=False, null=False)
    description = models.TextField(blank=True, null=True)

models.py (product):

class Product(models.Model):
    product_code = models.CharField(max_length=191, blank=False, null=False)
    name = models.CharField(max_length=191, blank=False, null=False)
    description = models.TextField(blank=False, null=False)
    price = models.DecimalField(max_digits=19, decimal_places=2)
    photo = models.ImageField(upload_to='pictures/products/', max_length=255, null=False, blank=False)
    category = models.name = models.ManyToManyField(Category)

How to achieve following result:

  {
        "categories": [
            {
                "id": 1,
                "name": "Indoor Muscle Training",
                "description": null,
                "products":{
                       "name":"product_name",
                       "code":"product_code"
                }
            },
            {
                "id": 2,
                "name": "Outdoor Muscle Training",
                "description": null,
                "products":{
                       "name":"product_name",
                       "code":"product_code"
                }
            }
        ]
  }

解决方案

using serializer-method field can be an option for this case. Our goal is get product information from category serializer. So for this

class CategorySerializer(serializers.ModelSerializer):
    products = serializers.SerializerMethodField()

    class Meta:
        model = Category
        fields = ('') # add relative fields

   def get_products(self, obj):
       products = obj.product_set.all() # will return product query set associate with this category
       response = ProductSerializer(products, many=True).data
       return response

这篇关于如何使用多对多关系在Django中反向序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆