How to get the difference of 2 different django models fields? - python

I have here 2 django database table
class ProductName(models.Model):
name = models.CharField(max_length=100)
class Inventory(models.Model):
product = models.ForeignKey(ProductName, on_delete = models.CASCADE )
qty = models.PositiveIntegerField()
class Sold(models.Model):
product = models.ForeignKey(ProductName, on_delete = models.CASCADE )
qty = models.PositiveIntegerField()
I would like to have an inventory table page wherein I can see the total qty left in Inventory (for example: Inventory.qty - Sold.qty) .
how to do this in Django ?

In your views file
from .model import Inventory, Sold
qty_left = Inventory.objects.count() - Sold.objects.count()

Related

Add product to cart using python in Django

I want to make a simple function that collects the product ID and adds it to the user's shopping cart by clicking the "add to cart" button. I have the shopping cart table and product table created but am struggling to find the correct function to add the product to the cart. Anything helps I am very new to Python and Django and am looking for some help to get me on the right track.
class Cart(models.Model):
cart_id = models.Charfield(primary_key=True)
total = models.DecimalField(max_digits=9,decimal_places=2)
quantity = models.IntegerField()
products = models.ManyToManyField(Product)
class Product(models.Model):
prod_id = models.CharField(max_length=15, null=True)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10,decimal_places=2)
description = models.TextField(max_length=1000)
def cart_add(request, prod_id):
item = Product.objects.get(pk=prod_id)
I know this isn't much to work with but anything helps, I am just looking for some help with creating the add to cart function
You need to have a relation between Cart model and User model, So that you can save product to respective user's cart.
Moreover a good approach would be adding another model named CartItem and keeping the product information in that model instead of storing product directly to Cart model. This is because when we add a product to cart, we might need to save and later update extra information about the product for example 'quantity'.
class Cart(models.Model):
cart_id = models.Charfield(primary_key=True)
total = models.DecimalField(max_digits=9,decimal_places=2)
quantity = models.IntegerField()
user = models.OneToOneField(User)
class CartItem(models.Model):
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
product_quantity = models.IntegerField(default=0)
user = models.OneToOneField(User)
So in this case, all you need to do is create a new CartItem object in add to cart view.:
def cart_add(request, prod_id, qty):
item = Product.objects.get(pk=prod_id)
cart_obj = Cart.objects.get(user=request.user)
CartItem.objects.create(cart=cart_obj, product=item, product_quantity=qty)
Hope this helps :)
You need a relation between Cart* and User
class Cart(models.Model):
cart_id = models.Charfield(primary_key=True)
total = models.DecimalField(max_digits=9,decimal_places=2)
quantity = models.IntegerField()
products = models.ManyToManyField(Product)
user = models.OneToOneField(User)
class Product(models.Model):
prod_id = models.CharField(max_length=15, null=True)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10,decimal_places=2)
description = models.TextField(max_length=1000)
def cart_add(request, prod_id):
item = Product.objects.get(pk=prod_id)
request.user.cart.add(item)

In Django, how to bind the dropdown list with data from another table using foreign key?

model.py
class LabTable(models.Model):
LabNo = models.AutoField(primary_key=True)
Pid = models.IntegerField()
Weight = models.IntegerField()
DoctorId = models.ForeignKey(DoctorTable,on_delete=models.CASCADE)
Date = models.DateField()
Category = models.CharField(max_length=50)
PatientType = models.CharField(max_length=50)
Amount = models.IntegerField()
class DoctorTable(models.Model):
DoctorId = models.AtuoField(primary_key = True)
DoctorName = models.CharField(max_length = 50)
Department = models.CharField(max_length = 100)
1.Here, I have created the lab model and a foreign key with doctor table. I want to use doctor table to present data in dropdown.
You can specify how to show a DoctorTable model object by implementing the __str__ method for that model, so:
class DoctorTable(models.Model):
DoctorId = models.AutoField(primary_key=True)
DoctorName = models.CharField(max_length=50)
Department = models.CharField(max_length=100)
def __str__(self):
return self.DoctorName
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be: doctor_name instead of DoctorName.
Note: Models normally have no Table suffix. A model is not a table, it is stored in a relational database as a table, but even then it has extra logic like validators, managers, etc.

How to show all objects with same field value as 1 result and aggregate another field in django

it is a little complicated, here is how my model looks like:
class Warehouse(models.Model):
in_move = "in"
out_move = "out"
move_type_choices = (
(in_move, _('دخول')),
(out_move, _('خروج'))
)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
branch = models.ForeignKey(Branch, on_delete=models.CASCADE)
quantity = models.DecimalField(max_digits=6, decimal_places=2)
move_type = models.CharField(choices=move_type_choices, max_length=120)
text = models.CharField(max_length=1200)
date = models.DateTimeField(auto_now=True)
employee = models.ForeignKey(Employee, on_delete=models.PROTECT)
what I need to do is :
A queryset like warehouse.objects.filter(branch=spicific_branch)
group it by the item name so it shows all the queryset that have
same item as one result
Get the sum of the field quantity
Define if it in or out move to get the current quantity inside
the warehouse
Try this way
branch_filter = warehouse.objects.filter(branch__name='spicific_branch')
item_sum = warehouse.objects.values('item__name' ).annotate(total_qty=Sum('quantity'))

Query to fetch highest rated x with mimimum x people rated

model.py
class Movie(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
vote_count = models.IntegerField()
class Watchlist(models.Model):
userid = models.IntegerField()
movie_id = models.ForeignKey(Movie, on_delete=models.CASCADE)
rating = models.IntegerField()
query to fetch top rated movie with minimum 5 people rated.
You should read basics about django's annotation.
In your case, something like this should work:
from django.db.models import Count, Sum, F
qs = Movie.objects.annotate(vote_count=Count('watchlist_set'),
vote_sum=Sum('watchlist_set__rating'),
rating = F('vote_sum')/F('vote_count')
).filter(
vote_count__gte=5,
).order_by(
'-rating'
)

Django complex filter by foreign key in Admin

Using Django 1.6
This is part of my model.
class Discount(models.Model):
discount_id = models.AutoField(primary_key=True)
discount_category = models.ForeignKey(Category)
discount_subcategory = models.ManyToManyField(Subcategory)
...
class Category(models.Model):
category_id = models.AutoField(primary_key=True)
category_name = models.CharField(max_length=50)
class Subcategory(models.Model):
subcategory_id = models.AutoField(primary_key=True)
parent_category = models.ForeignKey(Category, null=True, blank=True)
subcategory_name = models.CharField(max_length=25)
Whole of admin.py
from django.contrib import admin
from uygulama.models import *
admin.site.register([Company, Discount, Store, Category, City, Subcategory])
What I want:
I am adding Discount record. Meanwhile I select one parent category and I want to list all this parent category's subcategory. But I am getting this. When page open no selected category and listing all subcategories:
But I want to list only selected category's subcategories in Discount adding page. Not in Subcategory page.

Categories

Resources