Group By Django queryset by a foreignkey related field - django

I have a model Allotment
class Kit(models.Model):
kit_types = (('FLC', 'FLC'), ('FSC', 'FSC'), ('Crate', 'Crate'), ('PP Box', 'PP Box'))
kit_name = models.CharField(max_length=500, default=0)
kit_type = models.CharField(max_length=50, default=0, choices=kit_types, blank=True, null=True)
class AllotmentFlow(models.Model):
flow = models.ForeignKey(Flow, on_delete=models.CASCADE)
kit = models.ForeignKey(Kit, on_delete=models.CASCADE)
asked_quantity = models.IntegerField(default=0)
alloted_quantity = models.IntegerField(default=0)
class Allotment(models.Model):
transaction_no = models.IntegerField(default=0)
dispatch_date = models.DateTimeField(default=datetime.now)
send_from_warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
flows = models.ManyToManyField(AllotmentFlow)
For a stacked graph I am trying to get the data of different kit_type alloted in different months.
For that I have tried annotate but it isn't getting the desired results
dataset = Allotment.objects.all().annotate(
month=TruncMonth('dispatch_date')).values(
'month').annotate(dcount=Count('flows__kit__kit_type')).values('month', 'dcount')
Expected Output:
[{'month':xyz, 'kit_type':foo, count:123},...]
I am getting the month and count of kit type from above but how do I segregate it by kit_type?

having a field that represents your choice field names in this query is difficult
instead how about use the Count filter argument and annotate to get what you want
dataset = Allotment.objects.all().annotate(month=TruncMonth('dispatch_date')).values('month').annotate(
FLC_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="FLC")),
FSC_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="FSC")),
Crate_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="Crate")),
PP_Box_count=Count('flows__kit__kit_type', filter=Q(flows__kit__kit_type="PP_Box")),
).values('month', 'FLC_count', 'FSC_count', 'Crate_count', 'PP_Box_count')

Related

How to copy a object data to another object in Django?

I am trying to create an E-Commerce Website and I am at the Final Step i.e. Placing the Order. So, I am trying to add all the Cart Items into my Shipment model. But I am getting this error.
'QuerySet' object has no attribute 'product'
Here are my models
class Product(models.Model):
productId = models.AutoField(primary_key=True)
productName = models.CharField(max_length=200)
productDescription = models.CharField(max_length=500)
productRealPrice = models.IntegerField()
productDiscountedPrice = models.IntegerField()
productImage = models.ImageField()
productInformation = RichTextField()
productTotalQty = models.IntegerField()
alias = models.CharField(max_length=200)
url = models.CharField(max_length=200, blank=True, null=True)
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True, blank=True)
email = models.EmailField(max_length=100)
profileImage = models.ImageField(blank=True, null=True, default='profile.png')
phoneNumber = models.CharField(max_length=10, blank=True, null=True)
address = models.CharField(max_length=500, blank=True, null=True)
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
dateOrdered = models.DateTimeField(auto_now_add=True)
orderCompleted = models.BooleanField(default=False)
transactionId = models.AutoField(primary_key=True)
class Cart(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.IntegerField(default=0, blank=True, null=True)
dateAdded = models.DateTimeField(auto_now_add=True)
class Shipment(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
orderId = models.CharField(max_length=100)
products = models.ManyToManyField(Product)
orderDate = models.CharField(max_length=100)
address = models.CharField(max_length=200)
phoneNumber = models.CharField(max_length=13)
I just removed additional functions i.e. __str__ and others.
Here is the views.py
def orderSuccessful(request):
number = Customer.objects.filter(user=request.user).values('phoneNumber')
fullAddress = Customer.objects.filter(user=request.user).values('address')
timeIn = time.time() * 1000 # convert current time in milliSecond
if request.method == 'POST':
order = Shipment.objects.create(customer=request.user.customer, orderId=timeIn,
orderDate=datetime.datetime.now(), address=fullAddress,
phoneNumber=number)
user = Customer.objects.get(user=request.user)
preOrder = Order.objects.filter(customer=user)
orders = Order.objects.get(customer=request.user.customer, orderCompleted=False)
items = orders.cart_set.all() # Here is all the items of cart
for product in items:
product = Product.objects.filter(productId=items.product.productId) # error is on this line
order.products.add(product)
Cart.objects.filter(order=preOrder).delete()
preOrder.delete()
order.save()
else:
return HttpResponse("Problem in Placing the Order")
context = {
'shipment': Shipment.objects.get(customer=request.user.customer)
}
return render(request, "Amazon/order_success.html", context)
How to resolve this error and all the cart items to field products in Shipment model?
Your model is not really consistent at all. Your Cart object is an m:n (or m2m - ManyToMany) relationship between Product and Order. Usually, you would have a 1:n between Cart and Product (a cart contains one or more products). One Cart might be one Order (unless you would allow more than one carts per order). And a shipment is usually a 1:1 for an order. I do not see any of this relationships in your model.
Draw your model down and illustrate the relations between them first - asking yourself, if it should be a 1:1, 1:n or m:n? The latter can be realized with a "through" model which is necessary if you need attributes like quantities.
In this excample, we have one or more customers placing an order filling a cart with several products in different quantities. The order will also need a shipment fee.
By the way: bear in mind that "filter()" returns a list. If you are filtering on user, which is a one to one to a unique User instance, you would better use "get()" as it returns a single instance.
Putting in into a try - except or using get_object_or_404() makes it more stable.
product = Product.objects.filter(productId=items.product.productId)
should be something like:
product = product.product
not to say, it becomes obsolete.
It looks like you make a cart for a product by multiple instances of Cart, the problem is you try to access the wrong variable, also you don't need to filter again when you already have the instance, make the following changes:
carts = orders.cart_set.all() # Renamed items to carts for clarity
for cart in carts:
product = cart.product
order.products.add(product) # The name order is very misleading makes one think it is an instance of Order, actually it is an instance of Shipment
As mentioned above in my comment your variable names are somewhat misleading, please give names that make sense to any variable.

Is there a way i can join two tables and get the sum of duplicated field in Django

I have a model WarehouseTrade Account and WarehouseStorage Account
it look like this:-------
class WarehouseStorageAccount(models.Model):
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE, null=True)
item = models.ForeignKey(Item, on_delete=models.CASCADE, null=True)
grade = models.CharField(max_length=5, choices=ITEM_GRADE_CHOICES)
bags = models.IntegerField(default=0)
gross_weight = models.DecimalField(max_digits=20, decimal_places=3, default=0)
net_weight = models.DecimalField(max_digits=20, decimal_places=3, default=0)
updated = models.DateTimeField(auto_now=True)
class WarehouseTradeAccount(models.Model):
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE, null=True)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
grade = models.CharField(max_length=5, choices=ITEM_GRADE_CHOICES)
bags = models.IntegerField(default=0)
gross_weight = models.DecimalField(max_digits=20, decimal_places=3, default=0)
net_weight = models.DecimalField(max_digits=20, decimal_places=3, default=0)
updated = models.DateTimeField(auto_now=True)
I am trying to get All the data in both accounts, but it should sum it up if there is a duplicate between the two.
I have been able to achieve this with SQL using below code:
SELECT
data.warehouse_id AS Warehouse,
data.item_id AS Item,
data.grade AS Grade,
SUM((data.net_weight)) AS Net_WEIGHT,
SUM((data.gross_weight)) AS Gross_WEIGHT,
SUM((data.bags)) AS Bags
FROM
(SELECT warehouse_id, item_id, net_weight, grade, gross_weight, bags FROM public.inventory_warehousestorageaccount
UNION ALL
SELECT warehouse_id, item_id, net_weight, grade, gross_weight, bags
FROM public.inventory_warehousetradeaccount
) data
GROUP BY data.warehouse_id, data.item_id, data.grade
I tried using union to join the two tables, then get the aggregate the result, but I keep getting an error
wt = WarehouseTradeAccount.objects.all()
ws = WarehouseStorageAccount.objects.all()
w=wt.union(ws)
w.aggreate(new_net_weight=Sum('net_weight')
How do I replicate this in Django?
I actually didn't understand what do you mean by
it should sum it up if there is a duplicate between the two
Do you want to remove the duplicates?
attributes = ('warehouse','item','grade'....)
result = []
wt = WarehouseTradeAccount.objects.all()
ws = WarehouseStorageAccount.objects.all()
for wtobj, wsobj in zip(wt,ws):
for attribute in attributes:
if getattr(wtobj, attribute) != getattr(wsobj, attribute):
result.append(wtobj, wsobj)
break
else:
result.append(wtobj)

Django Joining Tables

I am trying to get the information from one table filtered by information from another table (I believe this is called joining tables).
I have these two models:
class Listing(models.Model):
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
price = models.DecimalField(max_digits=11, decimal_places=2, validators=[MinValueValidator(Decimal('0.01'))])
category = models.ForeignKey(Category, on_delete=models.CASCADE, default="")
imageURL = models.URLField(blank=True, max_length=500)
creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="creator", default="")
isOpen = models.BooleanField(default=True)
def __str__(self):
return f"{self.id} | {self.creator} | {self.title} | {self.price}"
class Watchlist(models.Model):
listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="listingWatched", default="")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="userWatching", default="")
What I need to do is to get all the listings from a specific user Watchlist, the idea is to generate a page with all of the information of each of the listings that are in the user's watchlist. What should I do?
Thanks in advance!
Since is a foreign key, in Django you can access the information by calling the attribute
For example:
my_user = Watchlist.objects.get(pk=1)
print(my_user.listing.title)
You can also access to that attrbute in a query in case you need to filter upwards some value
values = Watchlist.objects.all().filter(listing__title='MyTitle')
my_titles = [x.title for x in values]
print(my_titles)
Or in your case, if you want to list all the title for a specific user
values = Watchlist.objects.all().filter(user='foo_user')
my_titles = [x.listing.title for x in values]
print(my_titles)
More documentation here

How to specify GROUP BY field in Dajngo ORM?

I have the following working SQL statement:
SELECT id FROM ops_kpitarget WHERE (site_id = 1 AND validFrom <= "2019-08-28") GROUP BY kpi_id HAVING validFrom = (MAX(validFrom))
But I cannot get this to work inside Django ORM.
The best I got was the code below, but then the database is complaining that it is missing a GROUP BY clause to make HAVING work.
How can I get the same query with specifying "kpi_id" as the GROUP BY clause using Djangos ORM? Any ideas?
KpiTarget.objects
.filter(validFrom__lte=fromDate)
.values("id", "kpi")
.filter(validFrom=Max("validFrom"))
... which translates to:
SELECT "ops_kpitarget"."id", "ops_kpitarget"."kpi_id" FROM "ops_kpitarget" WHERE "ops_kpitarget"."validFrom" <= 2019-08-14 HAVING "ops_kpitarget"."validFrom" = (MAX("ops_kpitarget"."validFrom"))
I played around with annotate but this is not really giving me what I want...
Update:
Some background: I have 3 tables: Kpi, KpiTarget, and KpiTargetObservation.
Kpi holds all general information regarding the KPI like name, typeetc.
KpiTarget stores target values defined for several different sites. These target values can change over time. Hence, I have included the combination of MAX() and validFrom <= (some date) to determine the latest valid target for any given KPI.
KpiTargetObservation stores the individual observations per defined KPI target. It just holds the link to KpiTarget, the date of the observation, and the observation value.
The final queries I need to build will have to give me something like the following:
give me all known KPIs per given site
tell me the most recent target value for the KPIs you found
get me any known observation that is related to the identified kpi targets
I am struggling with the 2nd query, and specifically how to get this working using Djangos ORM. I could just escape to RAW SQL, but I would prefer to not to, if possible.
The models:
class KpiCategory(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Kpi(models.Model):
KPI_KIND_CHOICES = [("BOOL", "Boolean"), ("FLOAT", "Float"), ("STRING", "String")]
# firstCreated = models.DateField(auto_now_add=True)
# firstCreatedBy = models.OneToOneField(User, on_delete=models.CASCADE)
# lastEdited = models.DateField(auto_now=True)
# lastEditedBy = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
category = models.ForeignKey(KpiCategory, on_delete=models.CASCADE)
kind = models.CharField(max_length=150, choices=KPI_KIND_CHOICES)
def __str__(self):
return self.name
class KpiTarget(models.Model):
# firstCreated = models.DateField(auto_now_add=True)
# firstCreatedBy = models.OneToOneField(User, on_delete=models.CASCADE)
# lastEdited = models.DateField(auto_now=True)
# lastEditedBy = models.OneToOneField(User, on_delete=models.CASCADE)
kpi = models.ForeignKey(Kpi, on_delete=models.CASCADE, related_name="kpiTargetSet")
targetDouble = models.DecimalField(
max_digits=20, decimal_places=15, blank=True, null=True
)
targetBool = models.BooleanField(blank=True, null=True)
targetStr = models.CharField(max_length=255, blank=True)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
validFrom = models.DateField()
def __str__(self):
return str(self.kpi)
class KpiObservation(models.Model):
# firstCreated = models.DateField(auto_now_add=True)
# firstCreatedBy = models.OneToOneField(User, on_delete=models.CASCADE)
# lastEdited = models.DateField(auto_now=True)
# lastEditedBy = models.OneToOneField(User, on_delete=models.CASCADE)
kpiTarget = models.ForeignKey(
KpiTarget, on_delete=models.CASCADE, related_name="kpiObservationSet"
)
observed = models.DateField()
observationDouble = models.DecimalField(
max_digits=20, decimal_places=15, blank=True, null=True
)
observationBool = models.BooleanField(blank=True, null=True)
observationStr = models.CharField(max_length=255, blank=True)
def __str__(self):
return str(self.observed)
KpiTarget.objects.filter(validFrom__lte=fromDate).annotate(validFrom=Max("validFrom")).order_by('kpi__id').values("id", "kpi")

Querying from child of model given django inheritance and m2m link to parent

Among my models, I have Exercise which has a m2m link to Workout. I also have WorkoutPlan and LogBook which are types of Workouts. WorkoutPlan is where ideal workouts are stored. LogBook is where a user stores the workout they actually completed. They can also link a LogBook to a WorkoutPlan to indicate that the actual performance was connected to an original ideal plan.
class Exercise(NameDescModel):
muscles = models.ManyToManyField(Muscle, blank=True)
groups = models.ManyToManyField(Group, blank=True)
priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0)
frequency = models.IntegerField()
time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK)
last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now)
class Workout(NameDescModel):
exericises = models.ManyToManyField(Exercise, through='Measurement')
class WorkoutPlan(Workout):
priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0)
frequency = models.IntegerField()
time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK)
time_estimate = models.IntegerField()
last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now)
class LogBook(Workout):
workout_date = models.DateField(default=datetime.now)
notes = models.TextField(blank=True)
workout_plan = models.ForeignKey(WorkoutPlan, blank=True, null=True)
For a given exercise, I want to pull all of the WorkoutPlans that the exercise is in.
exercise_list = Exercise.objects.order_by('-last_p_calc_date')
for exercise in exercise_list:
print exercise
workout_list = []
for workout in exercise.workout_set.all():
workout_list.append(workout)
print list(set(workout_list))
print ""
I'm realizing that the list of workouts include both WorkoutPlans and LogBooks because exercise is attached to Workout, not to WorkoutPlans or LogBooks specifically.
How might I pull Workouts that are affiliated only to WorkoutPlans?
I think you've over-used inheritance here.
I guess you wanted to put the exercises field into a base model because WorkoutPlan and LogBook both have that field. But it seems like in reality WorkoutPlan and LogBook are different types of thing, rather than sub-types of Workout.
Possibly don't you need the exercises field on the LogBook model at all, since it has a foreign key to WorkoutPlan which seems a sensible place to record the exercises... unless you want to record the difference between the plan and exercises actually performed?
I would model it like this:
class Exercise(NameDescModel):
muscles = models.ManyToManyField(Muscle, blank=True)
groups = models.ManyToManyField(Group, blank=True)
priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0)
frequency = models.IntegerField()
time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK)
last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now)
class WorkoutPlan(Workout):
exercises = models.ManyToManyField(Exercise, through='Measurement')
priority_score = models.DecimalField(max_digits=5, decimal_places=3, editable=False, default = 0)
frequency = models.IntegerField()
time_period = models.CharField(max_length=2, choices=TIME_PERIOD_CHOICES,default=WEEK)
time_estimate = models.IntegerField()
last_p_calc_date = models.DateField("Date of Last Priority Recalculation", blank=True, null=True, default=datetime.now)
class LogBook(Workout):
exercises = models.ManyToManyField(Exercise, through='Measurement')
workout_date = models.DateField(default=datetime.now)
notes = models.TextField(blank=True)
workout_plan = models.ForeignKey(WorkoutPlan, blank=True, null=True)
You can then query either WorkoutPlans or LogBooks from an Exercise instance:
exercise_list = Exercise.objects.order_by('-last_p_calc_date')
for exercise in exercise_list:
print exercise
workout_list = exercise.workoutplan_set.all()
print ""