简体   繁体   English

如何在Django RestFramework中实现一对多关系?

[英]How to implement One to many Relationship in Django RestFramework?

Here i have mentioned my model.py and serilaizers.py i want to use one to many concept here. 在这里我提到了我的model.py和serilaizers.py我想在这里使用一对多的概念。 And my expecting output like this. 我期待这样的输出。

Expected output 预期产出

{
    "id": 1,        
    "product_name": "Rice",
    "description": "expired on 13-04-2018",
    "sales": "55",
    "cost": "55",
    "tax_details": [
        {
            'id': 1,
            'tax_name': "http://127.0.0.1:8000/tax/1/",
            'percentage': 10
        }, {
            'id': 2,
            'tax_name': "http://127.0.0.1:8000/tax/3/",
            'percentage': 2
        }, {
            'id': 3,
            'tax_name': "http://127.0.0.1:8000/tax/2/",
            'percentage': 05
        },
        ... 
    ],
}

Models.py Models.py

TAX model TAX模型

This is main tax table here i will mension Tax name like(IGST,GST,VAT) it was an Dropdown. 这是主要的税表这里我将维修税名如(IGST,GST,VAT)这是一个下拉列表。

Product 产品

Here it consist of product details and i have mentioned in Expected output 这里包含产品详细信息,我在预期输出中提到过

TaxProduct TaxProduct

In this model the entered taxname and percentage should store separate model. 在此模型中,输入的税名和百分比应存储单独的模型。

  class tax(models.Model)
      tax_name = models.CharField(max_length = 250)
      percentage = models.CharField(max_length = 250)

  class Taxproduct(models.Model):
      tax_name = ForeignKey(tax, on_delete = models.CASCADE)
      percentage = models.CharField(max_length = 3)

  class Product(models.Model):
      product_name =  models.CharField(max_length = 25)
      description = models.CharField(max_length = 150)
      category = models.ForeignKey(Category, on_delete = models.CASCADE)
      sales = models.CharField(max_length = 25)
      cost = models.CharField(max_length = 25)
      tax_details = models.CharField(max_length = 250)

Serializer 串行

  class TaxSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
      model = Tax
      fields = ('id','tax_name', 'tax_percentage')
  class TaxproductSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
      model = Taxproduct
      fields = ('id', 'tax_name', 'percentage')

  class ProductSerializer(serializers.HyperlinkedModelSerializer):
      tax_details = TaxproductSerializer(many=True, read_only = True)
  class Meta:
      model = Product
      fields = ('id', 'image','pro_name', 'description', 'sales', 'cost', 'tax_details')

so please tell me how to do this? 所以请告诉我怎么做?

First of all you need to add one-to-many relation to your models. 首先,您需要为模型添加一对多关系。 For this you need to add ForeignKkey field to Taxproduct model: 为此,您需要将ForeignKkey字段添加到Taxproduct模型:

class Taxproduct(models.Model)
    tax_name = models.CharField(max_length=250)
    percentage=models.CharField(max_length=250)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)

This will add to Product model attribute taxproduct_set which will return list of related taxes. 这将添加到Product模型属性taxproduct_set ,它将返回相关税的列表。 To add it to serializer with different name ( tax_details in your case) use field's source argument: 要将其添加到具有不同名称的序列化程序(在您的情况下为tax_details ,请使用字段的source参数:

class ProductSerializer(serializers.HyperlinkedModelSerializer):
    tax_details = TaxproductSerializer(many=True,read_only=True, source='taxproduct_set')
    class Meta:
        model = Product
        fields = ('id','pro_name','description','sales','cost','tax_details')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM