I have two model name ProductDetails and InventorySummary. I want to join this two model and wants to group with product name and SUM product quantity. The quantity should be multiplication with product price. My models are given bellow:
class ProductDetails(models.Model):
id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=100, unique=True)
purchase_price = models.DecimalField(max_digits=7, decimal_places=2)
dealer_price = models.DecimalField(max_digits=7, decimal_places=2)
retail_price = models.DecimalField(max_digits=7, decimal_places=2)
remarks = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.product_name
class InventorySummary(models.Model):
id = models.AutoField(primary_key=True)
date = models.DateField(default=date.today)
product_name = models.ForeignKey(ProductDetails, on_delete=models.CASCADE)
quantity = models.IntegerField()
def __str__(self):
return str(self.product_name)
My views are given bellow:
def stockPrice(request):
stock = InventorySummary.objects.select_related('product_name').
values('product_name').annotate(quantity=Sum('quantity'))
return render(request, 'inventory_price.html', {'stock': stock})
I can achieve this with the F expression. I'm not sure what you refer to by "product price", so I took purchase_price:
from django.db.models import Sum, F
stock = (
InventorySummary.objects.values("product_name__product_name")
.order_by("product_name__product_name")
.annotate(quantity=Sum(F("product_name__purchase_price") * F("quantity")))
)
Related
I'm creating an ecommerce store that sells T-shirts, hoodies, mugs, shot glasses, etc. For the t-shirts and hoodies there are sizes and sometimes color associated with each product. I'm trying to add multiple variations for each product. Here's my model.py code:
class Category(models.Model):
name = models.CharField(max_length = 255, db_index=True, null=True, blank=True)
slug = models.SlugField(max_length=255, unique=True, default='')
class Meta:
verbose_name_plural = 'categories'
def get_absolute_url(self):
return reverse('main:category_list', args=[self.slug])
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length = 255, default='')
description = models.TextField(blank=True)
image = models.ImageField(upload_to='products/', null=True, blank=True)
slug = models.SlugField(max_length = 255, default='')
price = models.DecimalField(max_digits=4,decimal_places=2)
update_defaults = models.BooleanField(default=False)
#inventory = models.DecimalField(max_digits=5, decimal_places=None)
class Meta:
ordering=(['name'])
def get_absolute_url(self):
return reverse('main:product_detail', args=[self.slug])
def __str__(self):
return self.name
class VariationManager(models.Manager):
def all(self):
return super(VariationManager, self).filter(active=True)
def sizes(self):
return self.all().filter(category='size')
def colors(self):
return self.all().filter(category='color')
VAR_CATEGORIES = (
('size', 'size'),
('color', 'color'),
)
class Variation(models.Model):
product = models.ForeignKey(Product, related_name="product_attrs", on_delete=models.CASCADE)
category = models.CharField(max_length=120, choices=VAR_CATEGORIES, default='size')
title = models.CharField(max_length=120)
price = models.DecimalField(max_digits=100, decimal_places=2, null=True, blank=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
objects = VariationManager()
def __str__(self):
return self.category + " " + self.title
def product_defaults(sender, instance, created, *args, **kwargs):
if instance.update_defaults:
categories = Category.objects.all()
print (categories)
for cat in categories:
print (cat.id)
if cat.id == 1: #for t-shirts
small_size = Variation.objects.get_or_create(product=instance,
category='size',
title='Small')
medium_size = Variation.objects.get_or_create(product=instance,
category='size',
title='Medium')
large_size = Variation.objects.get_or_create(product=instance,
category='size',
title='Large')
XL_size = Variation.objects.get_or_create(product=instance,
category='size',
title='XL')
DoubleXL_size = Variation.objects.get_or_create(product=instance,
category='size',
title='2XL')
TripleXL_size = Variation.objects.get_or_create(product=instance,
category='size',
title='3XL')
instance.update_defaults = False
instance.save()
post_save.connect(product_defaults, sender=Product)
The way it appears right now in my admin interface there is a name, attribute, value, and price (only named relevant fields for clarity). If I add a product like such: "t-shirt_1, size, sm, 17.98", then the next item I need to add is "t-shirt_1, size, med, 17.98" and so forth (2xl and above, price goes up). Is there a way to simplify this where I just enter the product name once, then add all sizes and associated pricing, as well as inventory tracking (haven't created field yet) for each size within the product?
edit:
I've edited my code. I got it figured out on the variations. Now I can't figure out how I could tie inventory quantities into it. If I put it in Product class, it's not specifying what size (i.e. 10 small, 8 medium, 12 Large).
Remove corresponding attribute fields from Product model and create OneToMany relationship between Product and ProductAttribute. Then create separate Size and Color models, and relate them to ProductAttribute with ManyToMany relationship:
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length = 255, default='')
description = models.TextField(blank=True)
image = models.ImageField(upload_to='products/', null=True, blank=True)
slug = models.SlugField(max_length = 255, default='')
price = models.DecimalField(max_digits=4,decimal_places=2)
has_attributes = models.BooleanField(default=False)
...
class ProductAttribute(models.Model):
product = models.ForeignKey('Product', related_name="product_attrs", on_delete=models.CASCADE)
sizes = models.ManyToManyField('Size', related_name="product_sizes", null=True, blank=True)
colors = models.ManyToManyField('Product', related_name="product_colors", null=True, blank=True)
...
class Size(models.Model):
size_num = models.CharField(max_length=10)
...
class Color(models.Model):
color_name = models.CharField(max_length=15)
...
Now you can create Product object, then go to ProductAttribute to relate corresponding product object with product attribute model and add each attribute of that product (sizes or colors). Whenever you need to get product size or color you can do it as follows:
# let's say you need to get sizes of last product
p = Product.objects.last()
sizes = p.product_attrs.sizes.all()
# ↑ returns queryset containing all sizes of product
Not required
class Attribute(models.Model):
name = models.CharField(max_length = 255, default='')
def __str__(self):
return self.name
Hello I have a question I have no idea how can i get total price of not iterable object in Django, I get this error:
TypeError at /cart
'OrderItem' object is not iterable
Here is my code, I would be pleased by any advise.
views.py
order_items = OrderItem.objects.filter(cart=cart)
order_items = OrderItem.objects.annotate(
sum=Sum(F('item__price') * F('quantity'))
).get(cart=cart)
order_items.total_price = order_items.sum
order_items.save(force_update=True)
models.py
class Item(Visits, models.Model):
title = models.CharField(max_length=150)
price = models.IntegerField(default=1000)
image = models.ImageField(upload_to='pictures', default='static/images/man.png')
description = models.TextField(default="Item")
visits = models.IntegerField(default=0)
class OrderItem(models.Model):
cart = models.ForeignKey('Cart', on_delete=CASCADE, null=True)
item = models.ForeignKey(Item, on_delete=CASCADE, null=True)
quantity = models.IntegerField(default=1)
total_price = models.IntegerField(default=1)
you can create function in you model and used The result on your view
like this:
class OrderItem(models.Model):
cart = models.ForeignKey('Cart', on_delete=CASCADE, null=True)
item = models.ForeignKey(Item, on_delete=CASCADE, null=True)
quantity = models.IntegerField(default=1)
# does not needed
# total_price = models.IntegerField(default=1)
#property
def get_total(self):
total = self.item.price * self.quantity
return total
I have a model CartItem that has a ForeignKey to a Product model.
Because from Product model I get the description, image, etc.
However, I want to have a method called sub_total that returns and integer. I use this to calculate total to be paid for this CartItem.
This sub_total method query a different model costo_de_los_productos using some of the properties of CartItem. like: self.product.category.name, self.product.name, self.size, self.quantity.
I need to return an Integer from sub_total method.
However, something is not right with me query, if I comment it and return 0 it works, but total is 0.
def sub_total(self):
product_price = costo_de_los_productos.objects.filter(category=self.product.category.name,
product = self.product.name,
size=self.size,
quantity=self.quantity).values_list("price", flat=True)
What could be wrong?
class CartItem(models.Model):
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
size = models.CharField(max_length=20, choices=TAMANIOS)
quantity = models.CharField(max_length=20, choices=CANTIDADES)
file = models.FileField(upload_to='files', blank=True, null=True)
comment = models.CharField(max_length=100, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
step_two_complete = models.BooleanField(default=False)
# def __str__(self):
# return str(self.id) + " - " + str(self.size) + " por " + str(self.quantity)
def sub_total(self):
product_price = costo_de_los_productos.objects.filter(category = self.product.category.name,
product = self.product.name,
size=self.size,
quantity=self.quantity).values_list("price", flat=True)
# print(type(product_price))
return product_price
costo_de_los_productos model:
class costo_de_los_productos(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
price = models.IntegerField(default=30)
size = models.CharField(max_length=20, choices=TAMANIOS)
quantity = models.CharField(max_length=20, choices=CANTIDADES)
product model:
class Product(models.Model):
name = models.CharField(max_length=250, unique=False)
slug = models.SlugField(max_length=250, unique=False)
description = models.TextField(blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
image = models.ImageField(upload_to='product', blank=True, null=True)
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def get_url(self):
return reverse('shop:ProdDetail', args=[self.category.slug, self.slug])
def __str__(self):
return '{}'.format(self.name)
category model:
class Category(models.Model):
name = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250, unique=True)
description = models.TextField(blank=True, null=True)
image = models.ImageField(upload_to='category', blank=True, null=True)
video = EmbedVideoField(null=True, blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def get_url(self):
return reverse('shop:allCat', args=[self.slug])
def __str__(self):
return '{}'.format(self.name)
Image of "costo_de_los_productos" from Admin Panel:
UPDATE 1
Cannot print anything after the product_price query.
def sub_total(self):
print("Enters Subtotal")
print(self.product.category.name)
print(self.product.name)
print(self.size)
print(self.quantity)
product_price = costo_de_los_productos.objects.filter(category=self.product.category.name,
product=self.product.name,
size=self.size,
quantity=self.quantity).values_list("price", flat=True)[0]
print("Line after product_price query")
print(type(product_price))
return product_price
Hard coding the values doesn't return expected integer:
def sub_total(self):
print("Enters Subtotal")
print(self.product.category.name)
print(self.product.name)
print(self.size)
print(self.quantity)
product_price = costo_de_los_productos.objects.filter(category="Stickers",
product="Stickers transparentes",
size="5cm x 5cm",
quantity=300).values_list("price", flat=True)[0]
print("Line after product_price query")
print(type(product_price))
return product_price
prints results:
Enters Subtotal
Stickers
Stickers transparentes
5cm x 5cm
300
I have custorder models in which im taking product and price as foreign key from other models Product and price . I want to calculate the total price of product just by increasing quantity. Im just building a rest api for order creation . Please help me how to do it.
Models.py
class Product(models.Model):
product_id = models.AutoField(primary_key=True)
product = ArrayField(models.CharField(max_length=200, blank=True))
def __str__(self):
return str(self.product)
class Price(models.Model):
product = models.ForeignKey('Product',on_delete=models.CASCADE)
price_id = models.AutoField(primary_key=True)
price = models.DecimalField(max_digits=50, decimal_places = 5, default=0)
def __str__(self):
return "%s" % self.price
class CustOrder(models.Model):
Customer_id = models.AutoField(primary_key=True)
CustomerName = models.CharField(max_length=200)
email = models.EmailField(max_length=70,blank=True, null= True, unique= True)
gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
phone = PhoneField(null=False, blank=True, unique=True)
landmark = models.PointField()
#landmark = models.TextField(max_length=400, help_text="Enter the landmark", default='Enter landmark')
houseno = models.IntegerField(default=0)
#product_name = models.CharField(max_length=200, choices=PRODUCT_CHOICES,default='Boneless chicken')
# product_id = models.ForeignKey(Product, on_delete=models.CASCADE,related_name='custorder_productid')
product = models.ManyToManyField(Product, blank=True,related_name='pricetag')
quantity = models.IntegerField(default=0)
# price_id = models.ForeignKey(Price)
price = models.ForeignKey(Price, on_delete=models.SET_NULL, null=True,related_name='pricetag')
#price = models.DecimalField(max_digits=50, decimal_places=5, default=48.9)
pay_method = models.CharField(max_length=200,choices=PAYMENT_CHOICES, default='RAZOR PAY')
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
area = models.ForeignKey(Area, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.CustomerName
How do I calculate the total with the existing model fields
class Book(models.Model):
school_id = models.ForeignKey(School)
name = models.CharField(max_length=100, default=None, blank=None,
unique=True)
class_name = models.CharField(max_length=50)
category = models.ForeignKey(Category)
bundle = models.CharField(max_length=20, unique=True)
particulars = models.CharField(max_length=50)
tax_code = models.CharField(max_length=50, default=None)
amount = models.FloatField()
tax_CGST = models.FloatField(default=0)
tax_SGST = models.FloatField(default=0)
total = models.FloatField()
def total(self):
return ((self.tax_SGST+self.tax_CGST)*self.amount)/100
def __str__(self):
return self.name
In the above code, I want the total function to calculate the total from the Tax and amount fields and add it to the total field in the database