1

I'm new to django and struggling, I look over google but couldn't find anything that can help.

Here is my model:

from django.db import models
# from django.contrib.auth.models import


class Order(models.Model):

    table_id = models.IntegerField(unique=True)
    meal = models.ManyToManyField('meals')

    # Meal = models.ForeignKey('Meals',on_delete=models.CASCADE)
    @property
    def total_price(self):
        price = self.meal.objects.all().aggregate(total_price=models.Sum('meals__price'))
        return price['total_price']

class Meals(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(decimal_places=2, max_digits=5)

Here is my serializer.py :

from rest_framework import serializers


from cafe_app import models
class MealSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Meals
        fields = ['id','name','price',]

class OrderSerializer(serializers.ModelSerializer):

    **meal = MealSerializer(read_only=True,many=True)**
    class Meta:
        model = models.Order
        fields = ['table_id','meal',]

When I comment meal = MealSerializer(read_only=True,many=True) line then it shows input as table_id and meal where meal values are as Meal Object (1), Mean Object (2) ... .

My questions :

  1. How to display meal Object value instead of it as object.
  2. How Can I use total_price method in my view/serializer.
  3. How to see the flow like how it flows from which class to which class call goes and what is type and value of structure that I received.

Thanks.

pkd
  • 471
  • 1
  • 6
  • 17
  • hi, pkd I know it is confusing for you. It is for most people. But it will get easier if you practice and read the documentation and tutorials. For point 2 check serializer method field. For 3 use debugger in pycharm. I cannot understand point 1. – zaphod100.10 Mar 06 '20 at 12:44

1 Answers1

1
  1. How to display meal Object value instead of it as object.

Use the __str__ method on Meal.

  1. How Can I use total_price method in my view/serializer.

Define the annotation in your view's queryset, then add the custom field to the serializer. Do not add it to your model unless it's a one-off thing. The way you have it is pretty inefficient as it'll generate many queries for a list view.

  1. How to see the flow like how it flows from which class to which class call goes and what is type and value of structure that I received.

Use a debugger in an IDE like PyCharm, PDB, or the classic print statements.

Below are the corrections I've suggested for 1 and 2.

# models.py
from django.db import models

class Order(models.Model):

    table_id = models.IntegerField(unique=True)
    meal = models.ManyToManyField('meals')


class Meals(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(decimal_places=2, max_digits=5)

    def __str__(self):
        # This method defines what the string representation an instance.
        return f'{self.name}: ${self.price}'


# serializers.py
from rest_framework import serializers


from cafe_app import models
class MealSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Meals
        fields = ['id','name','price',]


class OrderSerializer(serializers.ModelSerializer):
    total_price = serializers.FloatField(read_only=True)
    meal = MealSerializer(read_only=True,many=True)
    class Meta:
        model = models.Order
        fields = ['table_id','meal', 'total_price']


# views.py
class OrderView(ViewSet): # or View.

    queryset = Order.objects.annotate(
        total_price=Sum('meal__price')
    )
schillingt
  • 13,493
  • 2
  • 32
  • 34