Django querysets to subtract items - django

I have two query sets to get the values for stock_in and stock_out. How do i loop to subtract stock_out from stock_in
here are the querysets
stock_in = OrderItems.objects.values('drug').annotate(
quantity_received=Sum('quantity_received'),
).order_by('drug')
stock_out = RequestItems.objects.values('drug').annotate(
quantity_received=Sum('quantity_issued')
).order_by('drug')

Approach it from the Drug model. It makes it a bit easier on the query.
Drug.objects.annotate(
stock_in=Sum('orderitems_set__quantity_received'),
stock_out=Sum('requestitems_set__quantity_issued'),
).annotate(
current_stock=F('stock_in')-F('stock_out')
)
Or you might be able to do it in one. I'm not sure.
Drug.objects.annotate(
current_stock=Sum('orderitems_set__quantity_received') - Sum('requestitems_set__quantity_issued'),
)

def stock_list(request):
stock_in = OrderItems.objects.values('drug__drugTitle','drug__drugCode', 'drug__denom_quantity').order_by('drug').annotate(quant_in=Sum('quantity_received'))
stock_out = RequestItems.objects.values('drug__drugTitle', 'drug__drugCode', 'drug__denom_quantity').order_by('drug').annotate(quant_out=Sum('quantity_issued'))
stock_available=[]
index = 0
for item in stock_out:
if item is None:
remaining_stc = stock_in[index]['quant_in']
stock_available.append({'drug__drugTitle':stock_in[index]['drug__drugTitle'],'drug__drugCode':stock_in[index]['drug__drugCode'], 'drug__denom_quantity':stock_in[index]['drug__denom_quantity'], 'quantr':remaining_stc})
else:
remaining_stc = stock_in[index]['quant_in']-stock_out[index]['quant_out']
stock_available.append({'drug__drugTitle':stock_in[index]['drug__drugTitle'],'drug__drugCode':stock_in[index]['drug__drugCode'], 'drug__denom_quantity':stock_in[index]['drug__denom_quantity'], 'quantr':remaining_stc})
index = index + 1
context = {
"stock_available":stock_available,
}
return render(request, 'stock/stock_list.html', context)

Related

Django queryset for current year

I have my query written to get some thing from database and display on my website that query is getting all the data from db but what if i want to get the data particular to current year only
def get(self, request, *args, **kwargs):
filters = self.get_filters()
result = Model.objects.all().filter(*filters).distinct().aggregate(
t=Sum('t'),
b=Sum('b'),
)
result2 = Model.objects.all().filter(*filters).distinct().aggregate(
past_due=Sum('balance', filter=Q(due_date__lt=timezone.now()))
)
zero = Decimal('0.00')
total = result['t'] or zero
balance = result['b'] or zero
past_due = result2['past_due'] or zero
amount_received = t - b
I think filter=Q(due_date__year=datetime.now().date().year) should work for you, if I understand your question correctly.
This worked for me
result = Model.objects.all().filter(*filters, created__year=datetime.now().year).distinct().aggregate(
t=Sum('t'),
b=Sum('b'),
)
result2 = Model.objects.all().filter(*filters, created__year=datetime.now().year).distinct().aggregate(
past_due=Sum('balance', filter=Q(due_date__lt=timezone.now()))
)

How to merge QuerySets from the same model

I want to combine (|) a series of QuerySets:all_english_text in a loop:
tag_result = Tag.objects.get(id=all_tag[index])
all_english_text = tag_result.notes.all().values('english_text', 'id')
by the following def:
def list_by_tag_post(request):
# get POST
all_tag = request.POST.getlist('tag_list')
for index in range(len(all_tag)):
tag_result = Tag.objects.get(id=all_tag[index])
all_english_text = tag_result.notes.all().values('english_text', 'id')
result = list(chain(*all_english_text)) # Merging method
all_english_text = result
# to template
context = {'all_english_text': all_english_text, 'all_tag': all_tag}
return render(request, 'list_by_tag.html', context)
3 methods were tried, they all didn't work:
result = list(chain(*all_english_text)) # Merging method 1
result = chain(*all_english_text) # Merging method 2
result = all_english_text.union(*all_english_text))# Merging method 3
This should be possible in a single query.
Without knowing what Tag.notes is it may not be exactly right. I'm assuming it's a reverse relationship from a ForeignKey on a Note model (Note.tag)
all_english_text = Note.objects.filter(tag_id__in=all_tag).values('english_text', 'id')

Django: How to take my query out of my for loop

I got the following code, that contains N queries:
for qty in total_qty_bought:
product_id = qty["product"]
quantity = int(qty["quantity__sum"])
try:
method_title = (
self.shipment_model.get(order_id=qty["order_id"])
.method_title.replace("Hent-selv", "")
.strip()
)
To solve the issue I tried to take the method_title query out of the for loop like this:
quantity = 0
for qty in total_qty_bought:
quantity = int(qty["quantity__sum"])
method_title = (
self.shipment_model.get(order_id=total_qty_bought[0]['order_id'])
.method_title.replace("Hent-selv", "")
.strip()
)
Note! There will be a full refrence further down, to understand the bigger picture
The issue in my solution is, that I am hard choosing which dict to enter , as I select [0] before order_id, and not in a for loop like before, would be selecting every individual item in the loop.
Is there a more sufficient way to do this? I do not see a solution without the for loop, but django debugger tool tells me it creates 2k+ queries.
CODE FOR REFRENCE
class InventoryStatusView(LoginRequiredMixin, View):
template_name = "lager-status.html"
cinnamon_form = CinnamonForm(prefix="cinnamon_form")
peber_form = PeberForm(prefix="peber_form")
pc_model = InventoryStatus
product_model = Product.objects.all()
order_item_model = WCOrderItem.objects.all()
shipment_model = WCOrderShipment.objects.all()
def get(self, request):
# Get all added objects that hasn't been deleted
objects = self.pc_model.objects.filter(is_deleted=False)
# Get all added objects that has been deleted
deleted_objects = self.pc_model.objects.filter(is_deleted=True)
# Sum all cinnamon that isn't deleted
total_cinnamon = (
self.pc_model.objects.filter(is_deleted=False)
.aggregate(Sum("cinnamon"))
.get("cinnamon__sum", 0.00)
)
# Sum all peber that isn't deleted
total_peber = (
self.pc_model.objects.filter(is_deleted=False)
.aggregate(Sum("peber"))
.get("peber__sum", 0.00)
)
# Get the amount of kilo attached to products
product_data = {}
queryset = ProductSpy.objects.select_related('product')
for productSpy in queryset:
product_data[productSpy.product.product_id] = productSpy.kilo
# Get quantity bought of each product
total_qty_bought = self.order_item_model.values(
"order_id", "product"
).annotate(Sum("quantity"))
# Get the cities from the inventory model
cities = dict(self.pc_model.CITIES)
# Set our total dict for later reference
our_total = {}
product = Product.objects.filter(
product_id__in={qty['product'] for qty in total_qty_bought}
).first()
# Check if we deal with kanel or peber as a product based on slug
index = 0
if product.slug.startswith("kanel-"):
index = 0
elif product.slug.startswith("peber-"):
index = 1
else:
pass
try:
# Sum the total quantity bought
quantity = 0
for qty in total_qty_bought:
quantity = int(qty["quantity__sum"])
# Get the inventory the order is picked from based on shipment method title
method_title = (
self.shipment_model.get(order_id=total_qty_bought[0]['order_id']) # The error
.method_title.replace("Hent-selv", "")
.strip()
)
# If the order isn't picked, but sent, use this inventory
if method_title not in cities.values():
method_title = "Hovedlager"
try:
# Get the total of kanel and peber bought
kilos = quantity * product_data[product.id]
# If there is no inventory, set it to 0 peber and 0 kanel
if method_title not in our_total:
our_total[method_title] = [0, 0]
# Combine place and kilos
our_total[method_title][index] += kilos
except KeyError as ex:
print(ex)
pass
except WCOrderShipment.DoesNotExist as ed:
print(ed)
pass
# Quantities BOUGHT! (in orders!)
print(our_total)
context = {
"cinnamon_form": self.cinnamon_form,
"peber_form": self.peber_form,
"objects": objects,
"deleted_objects": deleted_objects,
"total_cinnamon": total_cinnamon,
"total_peber": total_peber,
"our_total": our_total,
}
return render(request, self.template_name, context)
You can only do one query by using the __in operator:
shipments = self.shipment_model.get(order_id__in=list_containing_order_ids)
Then you can do a normal for loop in which you verify that condition.

django filter queryset show variables on template [duplicate]

This question already has an answer here:
Django-Filter Package: How To Filter Objects To Create Stats And Not Lists
(1 answer)
Closed 2 years ago.
Below is the views.py to my stats page. This page has a bunch of calculations based on my model objects. Works great. However when I apply django-filter to the data it does not change. Example filtering for only "short" trades or in the "last 7 days".
I know that get_context_data is basically hardcoding the results and it will not be affected by any filter query. This is not the approach but I've tried several things with no results so back to square one... How would I do this?
I've tried kwargs.update and context.update but also was not getting results.
This seems like it should be super obvious as how else do people show and filter statistics on objects?
views.py
class StatsView(LoginRequiredMixin, FilterView):
model = Trade
template_name = 'dashboard/stats.html'
filterset_class = StatsFilter
def get_form(self, *args, **kwargs):
form = StatsFilter()
user = self.request.user
form.fields['associated_portfolios'].queryset = Portfolio.objects.filter(user=user)
return form
def get_max_consecutive_wins(self, data):
longest = 0
current = 0
for num in data:
if num > 0:
current += 1
else:
longest = max(longest, current)
current = 0
return max(longest, current)
def get_max_consecutive_loses(self, data):
longest = 0
current = 0
for num in data:
if num < 0:
current += 1
else:
longest = max(longest, current)
current = 0
return max(longest, current)
def get_context_data(self, *args, **kwargs):
trade = Trade.objects.filter(user=self.request.user, status='cl').order_by('created')
all_trades = Trade.objects.filter(user=self.request.user, status='cl').count()
context = super(StatsView, self).get_context_data(*args, **kwargs)
data = [t.profit_loss_value_fees for t in trade]
win_trades_count = [t.trade_result for t in trade].count('win')
loss_trades_count = [t.trade_result for t in trade].count('loss')
scratch_trades_count = [t.trade_result for t in trade].count('scratch')
avg_win = 0 if win_trades_count == 0 else mean(t.profit_loss_value_fees for t in trade if t.trade_result == 'win')
avg_loss = 0 if loss_trades_count == 0 else mean(t.profit_loss_percent for t in trade if t.trade_result == 'win')
avg_win_percent = 0 if win_trades_count == 0 else mean(t.profit_loss_percent for t in trade if t.trade_result == 'win')
avg_loss_percent = 0 if loss_trades_count == 0 else mean(t.profit_loss_percent for t in trade if t.trade_result == 'loss')
context['all_trades'] = all_trades
context['gross_profit'] = sum([t.profit_loss_value for t in trade])
context['net_profit'] = sum([t.profit_loss_value_fees for t in trade])
context['win_trades_profit'] = sum(
t.profit_loss_value_fees for t in trade if t.trade_result == 'win')
context['loss_trades_profit'] = sum(
t.profit_loss_value_fees for t in trade if t.trade_result == 'loss')
context['win_trades_count'] = win_trades_count
context['loss_trades_count'] = loss_trades_count
context['scratch_trades_count'] = scratch_trades_count
context['win_trades_count_ratio'] = win_trades_count / all_trades * 100
context['loss_trades_count_ratio'] = loss_trades_count / all_trades * 100
context['scratch_trades_count_ratio'] = scratch_trades_count / all_trades * 100
context['total_fees'] = sum([t.get_fees() for t in trade])
context['avg_win'] = avg_win
context['avg_loss'] = avg_loss
context['avg_win_percent'] = avg_win_percent
context['avg_loss_percent'] = avg_loss_percent
context['avg_position_size'] = mean(t.position_size for t in trade)
context['largest_winning_trade'] = max([t.profit_loss_value_fees for t in trade])
context['largest_losing_trade'] = min([t.profit_loss_value_fees for t in trade])
context['largest_winning_trade_percent'] = max([t.profit_loss_percent for t in trade])
context['largest_losing_trade_percent'] = min([t.profit_loss_percent for t in trade])
context['max_consecutive_wins'] = self.get_max_consecutive_wins(data)
context['max_consecutive_loses'] = self.get_max_consecutive_loses(data)
context['qs'] = Trade.objects.filter(user=self.request.user, status='cl').order_by('created')
return context
The first problem, like you pointed, is that you are hard-coding the results in the get_context_data(). The FilterView ClassBasedView inherits from the MultipleObjectsMixin mixin of Django, so you should take both into account. I assume from your issues that you are not using the object_list property in your template - that's where the CBV populates the data.
Basically, in your current view, the flow will go as:
get the queryset based in the get_queryset() method.
If this doesn't exist, it will use the queryset property defined
In last case, the model. <-- This is your case, so it will use Trade.objects.all()
the FilterSet is applied to the original queryset
paginate (if you have it configured) and assign the filtered queryset to the object_list in the context.
So, some changes you can start trying:
Use the object_list to perform the stats calculation in your get_context_data() instead of querying for the Trade model yourself.
If you are always going to filter the results based in the current user, you can use the get_queryset() method to apply that filter from there, so you would do:
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(user=self.request.user, status='cl')
I would also recommend you to look at Django's built-in data aggregation mechanisms, as it would make all those calculations at DB level. If your start to treat big amounts of data, making all this manual processing is going to be an issue.
I think those points would be a good start!

Getting next and previous objects in Django

I'm trying to get the next and previous objects of a comic book issue. Simply changing the id number or filtering through date added is not going to work because I don't add the issues sequentially.
This is how my views are setup and it WORKS for prev_issue and does return the previous object, but it returns the last object for next_issue and I do not know why.
def issue(request, issue_id):
issue = get_object_or_404(Issue, pk=issue_id)
title = Title.objects.filter(issue=issue)
prev_issue = Issue.objects.filter(title=title).filter(number__lt=issue.number)[0:1]
next_issue = Issue.objects.filter(title=title).filter(number__gt=issue.number)[0:1]
Add an order_by clause to ensure it orders by number.
next_issue = Issue.objects.filter(title=title, number__gt=issue.number).order_by('number').first()
I know this is a bit late, but for anyone else, django does have a nicer way to do this, see https://docs.djangoproject.com/en/1.7/ref/models/instances/#django.db.models.Model.get_previous_by_FOO
So the answer here would be something something like
next_issue = Issue.get_next_by_number(issue, title=title)
Django managers to do that with a bit of meta class cleaverness.
If it's required to find next and previous objects ordered by field values that can be equal and those fields are not of Date* type, the query gets slightly complex, because:
ordering on objects with same values limiting by [:1] will always produce same result for several objects;
object can itself be included in resulting set.
Here's are querysets that also take into account the primary keys to produce a correct result (assuming that number parameter from OP is not unique and omitting the title parameter as it's irrelevant for the example):
Previous:
prev_issue = (Issue.objects
.filter(number__lte=issue.number, id__lt=instance.id)
.exclude(id=issue.id)
.order_by('-number', '-id')
.first())
Next:
next_issue = (Issue.objects
.filter(number__gte=issue.number, id__gt=instance.id)
.exclude(id=issue.id)
.order_by('number', 'id')
.first())
from functools import partial, reduce
from django.db import models
def next_or_prev_instance(instance, qs=None, prev=False, loop=False):
if not qs:
qs = instance.__class__.objects.all()
if prev:
qs = qs.reverse()
lookup = 'lt'
else:
lookup = 'gt'
q_list = []
prev_fields = []
if qs.query.extra_order_by:
ordering = qs.query.extra_order_by
elif qs.query.order_by:
ordering = qs.query.order_by
elif qs.query.get_meta().ordering:
ordering = qs.query.get_meta().ordering
else:
ordering = []
ordering = list(ordering)
if 'pk' not in ordering and '-pk' not in ordering:
ordering.append('pk')
qs = qs.order_by(*ordering)
for field in ordering:
if field[0] == '-':
this_lookup = (lookup == 'gt' and 'lt' or 'gt')
field = field[1:]
else:
this_lookup = lookup
q_kwargs = dict([(f, get_model_attr(instance, f))
for f in prev_fields])
key = "%s__%s" % (field, this_lookup)
q_kwargs[key] = get_model_attr(instance, field)
q_list.append(models.Q(**q_kwargs))
prev_fields.append(field)
try:
return qs.filter(reduce(models.Q.__or__, q_list))[0]
except IndexError:
length = qs.count()
if loop and length > 1:
return qs[0]
return None
next_instance = partial(next_or_prev_instance, prev=False)
prev_instance = partial(next_or_prev_instance, prev=True)
note that do not use object.get(pk=object.pk + 1) these sorts of things, IntegrityError occurs if object at that pk is deleted, hence always use a query set
for visitors:
''' Useage '''
"""
# Declare our item
store = Store.objects.get(pk=pk)
# Define our models
stores = Store.objects.all()
# Ask for the next item
new_store = get_next_or_prev(stores, store, 'next')
# If there is a next item
if new_store:
# Replace our item with the next one
store = new_store
"""
''' Function '''
def get_next_or_prev(models, item, direction):
'''
Returns the next or previous item of
a query-set for 'item'.
'models' is a query-set containing all
items of which 'item' is a part of.
direction is 'next' or 'prev'
'''
getit = False
if direction == 'prev':
models = models.reverse()
for m in models:
if getit:
return m
if item == m:
getit = True
if getit:
# This would happen when the last
# item made getit True
return models[0]
return False
original author
Usage
# you MUST call order by to pass in an order, otherwise QuerySet.reverse will not work
qs = Model.objects.all().order_by('pk')
q = qs[0]
prev = get_next_or_prev(qs, q, 'prev')
next = get_next_or_prev(qs, q, 'next')
next_obj_id = int(current_obj_id) + 1
next_obj = Model.objects.filter(id=next_obj_id).first()
prev_obj_id= int(current_obj_id) - 1
prev_obj = Model.objects.filter(id=prev_obj_id).first()
#You have nothing to loose here... This works for me