I want to find the difference between quantity of Product class and sold_quantity of Stock class. How do I do that
models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
quantity = models.IntegerField(null=True, blank=True)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
class Stock(models.Model):
sold_quantity = models.IntegerField(null=True, blank=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.product.name
For one instance you can do this by:
Example: id=1
product_instance = Product.objects.get(id=1)
quantity_difference = product_instance.quantity - product_instance.stock.sold_quantity
Note: This assumes the relationship is coupled between models.
Related
I have database and I want to extract specific data of specific user from queryset. Now i have this
VIEW
def index(request):
customerByName = Customer.objects.get(name='pablo')
shopListById = ShopList.objects.get(transaction_id=1)
shpoListSpecific = customerByName.shoplist_set.all()
specificProducts = shopListById.shoplistproduct_set.all()
context = {'customerByName':customerByName, 'shpoListSpecific':shpoListSpecific, 'shopListById':shopListById,
'specificProducts': specificProducts}
return render(request, 'QuickShopperApp/home.html', context)
MODELS
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
device = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
if self.name:
name = self.name
else:
name = self.device
return str(name)
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class ShopList(models.Model): # cart
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
#product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.id)
class ShopListProduct(models.Model): # each ShopList will have multiple ShopListProduct
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
shopList = models.ForeignKey(ShopList, on_delete=models.SET_NULL, null=True) #shoplistitem.shoplist
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.product)
template.html
<h3>specificProducts: {{specificProducts}}</h3>
On my side i see items of specific customer.
specificProducts: <QuerySet [<ShopListProduct: Apple>, <ShopListProduct: Cucumber>, <ShopListProduct: Cucumber>]>
How can i get only Apple, Cucumber, Cucumber?
try this
'specificProducts': specificProducts.values_list('product__name')
or
'specificProducts': list(specificProducts.values_list('product__name', flat=True))
https://docs.djangoproject.com/en/3.1/ref/models/querysets/#values-list
I'm getting __ str __ returned non-string (type Product) when I try to add OrderItem in my Django project
models.py:
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
date_ordered = models.DateField(auto_now_add=True, blank=True, null=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True)
def __str__(self):
return self.transaction_id
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
def __str__(self):
return self.product
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
image = models.ImageField(null=True, blank=True)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return str(self.name)
#property
def imageUrl(self):
try:
url = self.image.url
except:
url =''
return url
admin.py:
admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(Tag)
admin.site.register(OrderItem)
I get the same error when I try this:
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
image = models.ImageField(null=True, blank=True)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return 'test'
Sometimes I get this error when I try to add/delete items (in Orders, OrderItems, Products)
The problem is not the __str__ method of the Product class, but the __str__ method of the OrderItem class. self.product is not a string, but a Product object, so you can not return this in the __str__ method. You can for example return the str(…) of the product:
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
def __str__(self):
return str(self.product)
I want to query from OrderItem Model like
total_orders = OrderItem.objects.filter(product.user == request.user.id).count()
but i am getting error
**
NameError at /home
name 'product' is not defined
**
MY MODELS:
Product Model:
class Product(models.Model):
title = models.CharField(max_length=150)
user = models.ForeignKey(
User, blank=True, null=True, on_delete=models.SET_DEFAULT, default=None)
description = models.TextField()
price = models.FloatField()
quantity = models.IntegerField(default=False, null=True, blank=False)
minorder = models.CharField(
max_length=150, help_text='Minum Products that want to sell on per order', null=True, default=None, blank=True)
image = models.ImageField()
category = models.ForeignKey(
Categories, default=1, on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True)
def __str__(self):
return self.title
Order Item Model
class OrderItem(models.Model):
product = models.ForeignKey(
Product, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(
Order, on_delete=models.SET_NULL, blank=True, null=True)
quantity = models.FloatField(default=0, null=True, blank=True)
date_orderd = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
User, on_delete=models.SET_NULL, blank=True, null=True)
price = models.FloatField(blank=True, null=True)
def __str__(self):
return str(self.product)
My View:
def home(request):
total_orders = OrderItem.objects.filter(
product.user == request.user.id).count()
return render(request, "sellerprofile/home.html", {'total_orders': total_orders})
Do:
total_orders = OrderItem.objects.filter(product__user=request.user).count()
You can look at the documentation here about field lookups on more detail.
I have a product model. I need to autocomplete product name into input field. How can I code?
class Product(models.Model):
product_name = models.CharField(max_length=255)
product_detail = models.TextField(blank=True, null=True)
product_price = models.FloatField(blank=True)
category = models.CharField(max_length=30)
product_image = models.ImageField(upload_to='pictures/%Y/%m/%d/', max_length=255, blank=True, null=True)
product_amount = models.IntegerField(blank=True, null=True)
del_flag = models.BooleanField(default=False)
def __str__(self):
return self.product_name
I am new to django and i am facing a problem in displaying the average of time
My database model is
from django.db import models
class Employee(models.Model):
class Meta:
db_table = 'Employee'
emp_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100, blank=True, null=True)
department_id = models.IntegerField(blank=True, null=True)
position = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.name
class AttendanceRecord(models.Model):
class Meta:
db_table = 'AttendanceRecord'
emp_id = models.ForeignKey(Employee, on_delete=models.CASCADE)
# emp_id = models.CharField(max_length=100, blank=True, null=True)
date = models.CharField(max_length=100, blank=True, null=True)
miti = models.CharField(max_length=100, blank=True, null=True)
day = models.CharField(max_length=100, blank=True, null=True)
in_time = models.CharField(max_length=100, blank=True, null=True)
out_time = models.CharField(max_length=100, blank=True, null=True)
punch_count = models.IntegerField(blank=True, null=True)
time_spent = models.FloatField(blank=True, null=True)
difference_time = models.FloatField(blank=True, null=True)
def __str__(self):
return self.emp_id.name
class Department:
class Meta:
db_table = 'Department'
department_id = models.IntegerField(primary_key=True)
staff_count= models.IntegerField()
My Report view is:
I want to average the in_time so that i can average it and display in the report template. i cant figure out how.
from attendance.models import AttendanceRecord
class Report:
def get_all_employee_data(from_date, to_date):
all_employee_list = AttendanceRecord.objects.filter(miti__gt=from_date, miti__lt=to_date).order_by(
'-miti')
return all_employee_list
def get_specific_emp_data(from_date, to_date, e_id):
specific_employee_list = AttendanceRecord.objects.filter(miti__gt=from_date, miti__lt=to_date,
emp_id=e_id).order_by('-miti')
return specific_employee_list
My test data looks like this
TEST DATA