django-queryset

Django QuerySet vs Raw Query performance

痞子三分冷 提交于 2020-02-01 07:59:47
问题 I have noticed a huge timing difference between using django connection.cursor vs using the model interface, even with small querysets. I have made the model interface as efficient as possible, with values_list so no objects are constructed and such. Below are the two functions tested, don't mind the spanish names. def t3(): q = "select id, numerosDisponibles FROM samibackend_eventoagendado LIMIT 1000" with connection.cursor() as c: c.execute(q) return list(c) def t4(): return list

Django: annotate Sum Case When depending on the status of a field

偶尔善良 提交于 2020-01-31 18:14:45
问题 In my application i need to get all transactions per day for the last 30 days. In transactions model i have a currency field and i want to convert the value in euro if the chosen currency is GBP or USD. models.py class Transaction(TimeMixIn): COMPLETED = 1 REJECTED = 2 TRANSACTION_STATUS = ( (COMPLETED, _('Completed')), (REJECTED, _('Rejected')), ) user = models.ForeignKey(CustomUser) status = models.SmallIntegerField(choices=TRANSACTION_STATUS, default=COMPLETED) amount = models.DecimalField

Django: annotate Sum Case When depending on the status of a field

ぃ、小莉子 提交于 2020-01-31 18:14:09
问题 In my application i need to get all transactions per day for the last 30 days. In transactions model i have a currency field and i want to convert the value in euro if the chosen currency is GBP or USD. models.py class Transaction(TimeMixIn): COMPLETED = 1 REJECTED = 2 TRANSACTION_STATUS = ( (COMPLETED, _('Completed')), (REJECTED, _('Rejected')), ) user = models.ForeignKey(CustomUser) status = models.SmallIntegerField(choices=TRANSACTION_STATUS, default=COMPLETED) amount = models.DecimalField

Django: optimizing a query with spread data

青春壹個敷衍的年華 提交于 2020-01-25 02:24:15
问题 I have Order objects and OrderOperation objects that represent an action on a Order (creation, modification, cancellation). Conceptually, an order has 1 to many order operations. Each time there is an operation on the order, the total is computed in this operation. Which means when I need to find the total of an order, I just get the last order operation total. The simplified code class OrderOperation(models.Model): order = models.ForeignKey(Order) total = DecimalField(max_digits=9, decimal

Django Select Query Time Diff

风流意气都作罢 提交于 2020-01-24 15:14:45
问题 I am trying to query a database table in django with, among others, the following columns: id | start_time | end_time Rather than getting the separate values for the two, can I just get the difference directly in a query? Something to this effect: SELECT id, Diff(start_time, end_time) FROM myTable 回答1: QuerySet.extra() will allow you to specify arbitrary expressions for a column. Note that the result will be DB-dependent. 回答2: This can be done entirely through the ORM in Django 1.10 and above

Django: How do I use a string as the keyword in a Q() statement?

假如想象 提交于 2020-01-24 02:51:49
问题 I'm writing a simple search form for a certain model. Let's call the model Orchard and give it the attributes apples , oranges , and pears , just for the sake of demonstration. So, the form does not require all fields to be filled. So you can search on apples and oranges but not pears. I them need to filter like this: Orchard.objects.filter(apples=request.GET.get('apples'), oranges=request.GET.get('oranges'), pears=request.GET.get('pears')) but if pears is empty, no results will ever return.

What is simplest way join __contains and __in?

匆匆过客 提交于 2020-01-21 17:26:13
问题 I am doing tag search function, user could observe a lot of tags, I get it all in one tuple, and now I would like to find all text which include at least one tag from the list. Symbolic: text__contains__in=('asd','dsa') My only idea is do loop e.g.: q = text.objects.all() for t in tag_tuple: q.filter(data__contains=t) For example: input tuple of tags, ('car', 'cat', 'cinema') output all messages what contains at least one word from that tuple, so My cat is in the car , cat is not allowed in

How can I filter a date of a DateTimeField in Django?

血红的双手。 提交于 2020-01-18 04:13:12
问题 I am trying to filter a DateTimeField comparing with a date. I mean: MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22)) I get an empty queryset list as an answer because (I think) I am not considering time, but I want "any time". Is there an easy way in Django for doing this? I have the time in the datetime setted, it is not 00:00 . 回答1: Such lookups are implemented in django.views.generic.date_based as follows: {'date_time_field__range': (datetime.datetime.combine(date, datetime

How can I filter a date of a DateTimeField in Django?

我怕爱的太早我们不能终老 提交于 2020-01-18 04:11:11
问题 I am trying to filter a DateTimeField comparing with a date. I mean: MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22)) I get an empty queryset list as an answer because (I think) I am not considering time, but I want "any time". Is there an easy way in Django for doing this? I have the time in the datetime setted, it is not 00:00 . 回答1: Such lookups are implemented in django.views.generic.date_based as follows: {'date_time_field__range': (datetime.datetime.combine(date, datetime

Lookup hour on DateTimeField Django

跟風遠走 提交于 2020-01-17 04:39:05
问题 I have the model class Item(models.Model): inicio = models.DateTimeField() When I try to make this query: itens = Item.objects.filter(inicio__hour__gte=6) It returns me: FieldError Unsupported lookup 'hour' for DateTimeField or join on the field not permitted. How can I make this query? 回答1: You need to filter using a timedelta: from datetime import datetime, timedelta five_hours_ago = datetime.now() - timedelta(hours=5) items = Item.objects.filter(inicio__lt=five_hours_ago) You can always