Django Queryset .last() method on processed queryset returning incorrect element
Date : March 29 2020, 07:55 AM
With these it helps If anyone put any thought into this edge-case, the reasons would most likely be a combination of consistency and performance. First of all, you generally can't evaluate the entire queryset just to get the last element without imposing a huge performance penalty. Person.objects.order_by('order').last() should get one row, not the entire table -- which could contain millions of rows. So in case of an unevaluated queryset, you need to reverse the order in SQL and get the top element. This will always suffer from the problem you describe. people = Person.objects.order_by('order')
p1 = people.last()
bool(people)
p2 = people.last()
|
Django QuerySet.union() -or- QuerySet.raw() to Achieve Case-Insensitive Mutli-Search
Date : March 29 2020, 07:55 AM
wish helps you Given a URL like this: , Well, here's the answer. Use "Q" objects! :-) ...
foo = self.request.GET.getlist('foo')
if (len(foo) > 0):
q = Q()
for _foo in foo:
q |= Q(foo__iexact=_foo)
queryset = queryset.filter(q)
...
http://..../fl/users/?foo=&bar=some_bar
<QueryDict: {'foo': [''], 'bar': ['some_bar']}>
...
foo = self.request.GET.getlist('foo')
if (len(foo) > 0):
q = Q()
for _foo in foo:
if (_foo == ''):
q |= Q(foo=None)
else:
q |= Q(foo__iexact=_foo)
queryset = queryset.filter(q)
...
|
Django ORM DateTimeField ExpressionWrapper QuerySet annotation
Date : March 29 2020, 07:55 AM
I hope this helps . I have made it work by changing the query annotation to the following: sprints = Sprint.objects.annotate(
duration=models.Case(
models.When(
Q(started__isnull=False) &
Q(done__isnull=False),
then=(models.F('done') - models.F('started')),
),
models.When(
Q(started__isnull=False) &
Q(done__isnull=True),
then=(Now() - models.F('started')),
),
output_field=models.DurationField()
)).values_list('name',
'planned',
'started',
'done',
'duration')
|
how to implement ExpressionWrapper, F and Sum properly in django
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have two models Product and Damaged. Product has quantity of an item and Damaged has the number of damaged quantity of that item. In my template list_product I have listed total quantity of each product. When I add number of damaged_quantity into the Damaged model it should be deducted from the number of total quantity in Product. I have implemented the following code. Everything works fine. The only problem I am having is if the total number of quantity in Product is equal to number of damaged_quantity from Damaged it doesn't show 0 in the template of product_list. How to solve it? , You can try like this: products = Product.objects.annotate(damaged_product_quantity=Sum(
'damagedproducts__damaged_quantity')).annotate(
real_quantity=F('quantity')-F('damaged_product_quantity'))
from django.db.models import Value, When, Case
products = Product.objects.annotate(
damaged_product_quantity=Sum('damagedproducts__damaged_quantity'))
updated_product = products.annotate(
real_quantity = When(
Case(quantity__gt=F('damaged_product_quantity'), then=F('quantity')-F('damaged_product_quantity')),
Case(quantity__lte=F('damaged_product_quantity'), then=Value(0))
)
)
|
How to return multiple queryset object or add queryset result from get_queryset method in Django
Date : March 29 2020, 07:55 AM
|