简体   繁体   中英

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. 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

TAX model

This is main tax table here i will mension Tax name like(IGST,GST,VAT) it was an Dropdown.

Product

Here it consist of product details and i have mentioned in Expected output

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:

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. To add it to serializer with different name ( tax_details in your case) use field's source argument:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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