Complex DB query fun

2021-02-06 Thread Samuel Smith
I have a blog that is using django-taggit and on my list and detail views, I'd like to have a section to display other posts with related tags. At first I thought I could just use a correlated sub-select with annotate() along with the postgresql string_agg() to squeeze the related blog post's t

Django db query

2019-08-22 Thread Suraj Thapa FC
class courses(models.Model): level = ( ('beginner', 'Beginner Level'), ('intermediate', 'Intermediate Level'), ('expert', 'Expert Level'), ('all level', 'All Level'), ) type = ( ('live', 'LIVE'), ('on demand', 'On Demand'), )

Re: Optimizing DB query involving annotate and aggregate

2014-02-11 Thread ST
Managed it in the end, so for reference here is what I ended up with: def total_credit_debt(self): transaction_totals = Transaction.objects.filter(member__isnull=False).values('member').annotate(total=Sum('amount')).order_by() creditors = transaction_totals.filter(total__gt=0)

Re: Optimizing DB query involving annotate and aggregate

2014-02-06 Thread Arnold Krille
On Wed, 5 Feb 2014 10:11:29 -0800 (PST) ST wrote: > Hi, > > I'm trying to optimize the run-time of getting total credit and debt > values out of our database. Ideally I'd like to formulate it as a > Django query. This is the raw SQL query I have, which produces the > right answer and is very fast

Re: Optimizing DB query involving annotate and aggregate

2014-02-06 Thread Erik Cederstrand
Den 06/02/2014 kl. 12.34 skrev ST : > This didn't work - it produced a "SELECT FROM" query, which obviously didn't > work - tried adding 'amount' to the values_list, but that didn't help either. > Eventually got it to work by using .only('member', 'amount') instead, and it > *was* fast, but it

Re: Optimizing DB query involving annotate and aggregate

2014-02-06 Thread ST
On Wednesday, February 5, 2014 8:01:53 PM UTC, Anssi Kääriäinen wrote: > > Something like this might work: > > Transaction.objects.values_list('member_id').annotate(total=Sum('amount')).filter(total__gt=0).aggregate(Sum('total')) > This didn't work - it produced a "SELECT FROM" query, which

Re: Optimizing DB query involving annotate and aggregate

2014-02-05 Thread Anssi Kääriäinen
Something like this might work: Transaction.objects.values_list('member_id').annotate(total=Sum('amount')).filter(total__gt=0).aggregate(Sum('total')) That is, don't start from Member, Django isn't smart enough to get rid of the non-necessary joins. Instead go directly for the query you wrot

Optimizing DB query involving annotate and aggregate

2014-02-05 Thread ST
Hi, I'm trying to optimize the run-time of getting total credit and debt values out of our database. Ideally I'd like to formulate it as a Django query. This is the raw SQL query I have, which produces the right answer and is very fast (milliseconds): SELECT sum(tg.total) FROM ( SELECT sum

Re: How to get hold of the failed db query

2011-05-23 Thread Thomas Larsen Wessel
Thanks Michael. I have been looking into the log today. The problem is however, that the database server is a different computer which I don't have direct access to. It requires emailing another guy every time I need the log. I think that django should be able to tell when one query fails, right?

Re: How to get hold of the failed db query

2011-05-22 Thread Michael Radziej
Hi Thomas, there are multiple ways to catch the query. I suggest to enable query logging in your postgres server for failed statements. That's a good idea, anyway. See the "When to log" and "What to log" settings in the postgresql.conf file. Kind regards Michael -- -- You received this me

How to get hold of the failed db query

2011-05-20 Thread Thomas Larsen Wessel
I am running a site using Django 1.2.5 and postgres. I just upgraded Django and Django CMS to their latest versions using South. And now I get a MOD_PYTHON error. Here is an excerpt: MOD_PYTHON ERROR (...) File "/home/wsc2/lib/Django-1.2.5/django/db/backends/__init__.py", line 56, in _savepoin

Re: Simplification of a DB "query"

2010-06-11 Thread Dan Harris
This is totally off the top of my head and may not compile or work :) But something like this might be what you're looking for: # Example Model class MyModel(models.Model): start_time = models.TimeField() # Example query from django.db.models import Q from django.db.model import Max # Get th

Re: Simplification of a DB "query"

2010-06-11 Thread Cesar Devera
I'm not sure how to do directly in Django ORM, but if you use plain SQL queries, you could do try: select * from conference_room cr1 where cr1.start_time >= (select max(start_time) from conference_room cr2 where cr2.start_time < :yout_filter_time) and cr1.start_time < :your_filter_time

Re: Simplification of a DB "query"

2010-06-10 Thread Kenneth Gonsalves
On Thursday 10 June 2010 19:26:33 illuminated wrote: > Although the above algorithm would/should work, I was wondering if > there was more elegant way to this? > without an endtime, it is going to be difficult -- Regards Kenneth Gonsalves Senior Associate NRC-FOSS at AU-KBC -- You received thi

Simplification of a DB "query"

2010-06-10 Thread illuminated
Hi all, I'm writing a django app and need help with filtering results from DB. There is a conference room and it's usage is stored in django model. There is a field "start_time" (model.TimeField); no "end_time". When I query the table with a time range (i.e. 3pm - 5pm) I would like to get not onl

Re: Inconsistency between DB Query Results Between Threads

2010-04-28 Thread Jared Smith
Thanks for the advice it appears that the issue was related to the old transaction sticking around. I put: @transaction.commit_on_success On top of the method and it looks like I'm no longer getting stale data; guess I'll have to go do this around all data retrieval methods. I was assuming I nee

Re: Inconsistency between DB Query Results Between Threads

2010-04-28 Thread Tomasz Zieliński
On 28 Kwi, 22:04, Jared Smith wrote: > Using Django DB API I have two threads one that increments a counter getting > stored to the database and then another thread that is reading this counter. > > T1(Thread 1) increments > T2(Thread 2) reads > > I have found that if I increment and store the c

Inconsistency between DB Query Results Between Threads

2010-04-28 Thread Jared Smith
Using Django DB API I have two threads one that increments a counter getting stored to the database and then another thread that is reading this counter. T1(Thread 1) increments T2(Thread 2) reads I have found that if I increment and store the counter value in T1 and then if I fetch it in the sa

Re: DB query filter on member relations

2008-02-02 Thread Sebastjan Trepca
with two fields, a url and a title. > > If no title is given, it is initialized as the url (or at least the > first 200 characters of the url). > > Is there a way to filter a DB query to get the rows that weren't > initialized with a title? > > Something like > &g

DB query filter on member relations

2008-02-01 Thread [EMAIL PROTECTED]
I have a Model with two fields, a url and a title. If no title is given, it is initialized as the url (or at least the first 200 characters of the url). Is there a way to filter a DB query to get the rows that weren't initialized with a title? Something like MyModel.objects.f

Re: DB Query

2006-12-04 Thread [EMAIL PROTECTED]
Found it - a custom manager: http://www.djangoproject.com/documentation/model_api/#custom-managers --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-

DB Query

2006-12-04 Thread Bret Walker
How can one perform a database query from an overloaded admin save function? I want to retrieve an old value, compare it to the one currently being saved, then perform an action based on the result of the comparison. Thanks, Bret --~--~-~--~~~---~--~~ You receiv