Issue
I was trying to implement a simple logic in my e-commerce website where I will be calculating total cart price by looping through orders and getting their price with the help of get_final_price but suddenly this error occurred so please help me
Function due to which error is occurring
def get_total(self):
total_sum = 0
for order_item in self.items.all():
total_sum += order_item.get_final_price()
return total_sum
Models.py file
from django.db import models
from django.conf import settings
from django.db.models.deletion import CASCADE
from django.db.models.fields import DateTimeField
from django.urls import reverse
# # Create your models here.
CATEGORY_CHOICES = (
('S', 'Shirts'),
('SW', 'Sports wear'),
('OW', 'Outwear')
)
LABEL_CHOICES = (
('P', 'primary'),
('S', 'secondary'),
('D', 'danger')
)
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True,null=True)
category = models.CharField( choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField( choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("product", kwargs={
'slug' : self.slug
})
def get_add_to_cart_url(self):
return reverse("add_to_cart", kwargs={
'slug' : self.slug
})
def get_remove_from_cart_url(self):
return reverse("remove_from_cart", kwargs={
'slug' : self.slug
})
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default = 1)
def __str__(self):
return f"{self.quantity} of {self.item.title}"
def get_total_item_price(self):
return self.quantity*self.item.price
def get_total_discount_item_price(self):
return self.quantity*self.item.discount_price
def get_amount_saved(self):
return self.get_total_item_price() - self.get_total_discount_item_price()
def get_final_price(self):
if self.item.discount_price:
return self.get_total_discount_item_price
return self.get_total_item_price
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
items = models.ManyToManyField(OrderItem)
def __str__(self):
return self.user.username
def get_total(self):
total_sum = 0
for order_item in self.items.all():
total_sum += order_item.get_final_price()
return total_sum
Error that occured
Solution
In your OrderItem
class, your get_final_price()
method is returning a function, not a value. Instead of returning a function, you need to return a value like this:
def get_final_price(self):
if self.item.discount_price:
discount_price = self.get_total_discount_item_price()
return discount_price
item_price = self.get_total_item_price()
return item_price
Answered By - Naeem Khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.