Python – How to establish one-to-many nested relationships using the django rest framework?

How to establish one-to-many nested relationships using the django rest framework?… here is a solution to the problem.

How to establish one-to-many nested relationships using the django rest framework?

Here I mentioned my model.py and serilaizers.py and I want to use the concept of one-to-many here. My expected output is 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': 20},
                  {'id': 3, 'tax_name': "http://127.0.0.1:8000/tax/2/", 'percentage': 05},
          ... ],
      }

Model .py

Tax model

This is the main tax table here, I will mention the tax name, e.g. (IGST, GST, VAT), which is a drop-down list.

products

The product details are included here, which I mentioned in the expected output

Tax products

In this model, the tax names and percentages entered should be stored in a 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)

This is my frontend screen

/image/aj7oF.png

So please tell me what to do?

Solution

According to what we discussed in the comments, you need something like this:

class Tax(models. Model)
    name = models. CharField(max_length=250)
    percentage = models. CharField(max_length=250)

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)

class TaxProduct(models. Model):
    tax = ForeignKey(Tax, on_delete=models. CASCADE)
    percentage = models. CharField(max_length=3)
    product = models. ForeignKey(Product, on_delete=models. CASCADE)
                                related_name="tax_details")

This is roughly a many-to-many relationship through tables.

The product will have an tax_details field that references the tax and its effective percentage, as you asked. The DRF JSON output is pretty much what you want.

Related Problems and Solutions