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 :
- How to display meal Object value instead of it as object.
- How Can I use total_price method in my view/serializer.
- 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.