Re: Query help

2021-09-21 Thread J.T.
Makes sense, I probably should have realized this from the beginning. Appreciate the help. On Monday, September 20, 2021 at 6:17:05 PM UTC-5 Sean wrote: > Add a property to count Y for each column > > Or add a computed field that tallies that on save > > For reference > > > https://stackoverflo

Re: Query help

2021-09-20 Thread Lalit Suthar
Sum, Count works when we try to get sum/count for a column, for this case you can try something like this def get_yes_count_by(id): user = User.objects.get(id=id) count = 0 for val in user.__dict__.values(): if val == "YES": count += 1 return count On Tue, 21 S

Re: Query help

2021-09-20 Thread Sean Collins
Add a property to count Y for each column Or add a computed field that tallies that on save For reference https://stackoverflow.com/questions/1454727/do-properties-work-on-django-model-fields On Mon, Sep 20, 2021 at 6:41 PM J.T. wrote: > I have a model called member that has 6 columns: ID,

Query help

2021-09-20 Thread J.T.
I have a model called member that has 6 columns: ID, Value1, Value2, Value3, Value4, Value5 The values in columns Value1, Value2, Value3, Value4, Value5 are either Y or N. I'm trying to query by ID to count the number of Y values for each member. For example one row would be: 5, Y, N, Y, N, N

RE: Database ORM query Help

2019-10-26 Thread dvdcedar
) status = models.CharField(max_length=25, choices=STATUS_TYPES, default=PENDING) From: django-users@googlegroups.com On Behalf Of wd Sent: Saturday, 26 October 2019 11:43 AM To: django-users@googlegroups.com Subject: Re: Database ORM query Help I think you could conside

Re: Database ORM query Help

2019-10-25 Thread wd
I think you could consider using raw sql, https://docs.djangoproject.com/en/2.2/topics/db/sql/#executing-custom-sql-directly On Fri, Oct 25, 2019 at 7:56 PM Cheda me wrote: > Hey guys, > > I have worked out the SQL query I would like to implement but am currently > struggling to create the Djang

Re: Database ORM query Help

2019-10-25 Thread Samiul Sk
Please post the code of Model and explain what do you want as Queryset? On Fri, 25 Oct 2019 at 17:26, Cheda me wrote: > Hey guys, > > I have worked out the SQL query I would like to implement but am currently > struggling to create the Django query. I have been attempting to use the > .aggregat

Database ORM query Help

2019-10-25 Thread Cheda me
Hey guys, I have worked out the SQL query I would like to implement but am currently struggling to create the Django query. I have been attempting to use the .aggregate() & Max() methods but cant seem to get the right result. Any help is much appropriated. SELECT temp.* FROM item_detail temp I

Re: Query help

2019-09-12 Thread Matt
Tosin, Please refer below. This should help. https://docs.djangoproject.com/en/2.2/topics/db/queries/ best, matt On Thu, Sep 12, 2019 at 10:01 AM Tosin Ayoola wrote: > Halo guyz I actually need a little help, I'm working on a airline tickets > reservations project using postman Api that requir

Query help

2019-09-12 Thread Tosin Ayoola
Halo guyz I actually need a little help, I'm working on a airline tickets reservations project using postman Api that requires making query for available flight going to a particular destination, from a particular city, arrival date & departure date & I got no idea on how to write the query, Pls

Table2 populated by raw SQL query, help needed

2013-01-17 Thread Andy Woods
Hi, I need to use a raw SQL query and populate a table2 table with the results. The query works fine and returns a list of Dicts using this function from the helpfile : def dictfetchall(cursor): "Returns all rows from a cursor as a dict"

Re: Django ORM - query help

2012-04-12 Thread Russell Keith-Magee
On Thursday, 12 April 2012 at 10:47 PM, Andre Terra wrote: > On Thu, Apr 12, 2012 at 10:01 AM, David (mailto:cthl...@gmail.com)> wrote: > > Log.objects.distinct('thing__id').order_by('thing__id', > > '-modified_on').select_related().filter(thing__deleted=0)[:20] > > > > By avoiding the use of

Re: Django ORM - query help

2012-04-12 Thread Andre Terra
On Thu, Apr 12, 2012 at 10:01 AM, David wrote: > Log.objects.distinct('thing__id').order_by('thing__id', > '-modified_on').select_related().filter(thing__deleted=0)[:20] > > By avoiding the use of values() I was able to then use the result as an > object and access everything I needed. > > The ab

Re: Django ORM - query help

2012-04-12 Thread akaariai
On Apr 12, 4:01 pm, David wrote: > The above ORM statement however does not look as elegant to read as I have > come to expect from Django though. The resulting SQL doesn't seem too > shabby however. .distinct(fields) + .order_by() is pretty low level stuff - that is why it would be nice if there

Re: Django ORM - query help

2012-04-12 Thread David
Thank you akaariai That put me on the right track. Log.objects.distinct('thing__id').order_by('thing__id', '-modified_on').select_related().filter(thing__deleted=0)[:20] By avoiding the use of values() I was able to then use the result as an object and access everything I needed. The above OR

Re: Django ORM - query help

2012-04-12 Thread Jani Tiainen
12.4.2012 11:51, David kirjoitti: Hi Jani That was very helpful. Is there a way to include select_related into that query? or do I have to list every single field I would like to return using values()? last_deleted = ModificationLog.objects.values('thing__id', ' thing __prefix', ' thing __first

Re: Django ORM - query help

2012-04-12 Thread akaariai
On Apr 12, 11:52 am, David wrote: > > Hi Jani > > > That was very helpful. Is there a way to include select_related into that > > query? or do I have to list every single field I would like to return using > > values()? > > > last_deleted = ModificationLog.objects.values('thing__id', ' > > thing__

Re: Django ORM - query help

2012-04-12 Thread David
> > Hi Jani > > That was very helpful. Is there a way to include select_related into that > query? or do I have to list every single field I would like to return using > values()? > > last_deleted = ModificationLog.objects.values('thing__id', ' > thing__prefix', ' thing __first_name', ' thing__

Re: Django ORM - query help

2012-04-12 Thread David
Hi Jani That was very helpful. Is there a way to include select_related into that query? or do I have to list every single field I would like to return using values()? last_deleted = ModificationLog.objects.values('thing__id', ' thing __prefix', ' thing __first_name', ' thing__last_name', ' t

Re: Django ORM - query help

2012-04-11 Thread Jani Tiainen
11.4.2012 23:47, David kirjoitti: Thanks for your reply. I am grateful for your help. If you remember this question when you feel less sleepy I'd be very interested to see the inner join alternative :) Thanks again Well, after good night sleep I've some idea. Something like should do the t

Re: Django ORM - query help

2012-04-11 Thread David
Thanks for your reply. I am grateful for your help. If you remember this question when you feel less sleepy I'd be very interested to see the inner join alternative :) Thanks again -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this

Re: Django ORM - query help

2012-04-11 Thread Jani Tiainen
Hi, You're not doing anything wrong. The catch is that since "Thing" can exist without Log you will get outer join. If you want to get along with inner join, you should turn query around and start querying from Log model. I'm just too tired to think how it should be done right now... =) On Wed,

Django ORM - query help

2012-04-11 Thread David
class Log(models.Model): thing = models.ForeignKey(Thing) context = models.CharField(max_length=255) action = models.CharField(max_length=255) modifier = models.ForeignKey(User, limit_choices_to={'groups__in': [2]}) modified_on = models.DateTimeField(auto_now=True) class Thing(

Re: Complex Query help - get items from the org to which the user belongs

2011-07-07 Thread Venkatraman S
On Thu, Jul 7, 2011 at 3:49 PM, DrBloodmoney wrote: > On Thu, Jul 7, 2011 at 12:04 AM, Venkatraman S wrote: > > I am doing some bechmarking on the performance of both the versions of > the > > query: > > 1. > > > Item.objects.filter(created_by__employees__org__in=u.employees_set.all().values_lis

Re: Complex Query help - get items from the org to which the user belongs

2011-07-07 Thread DrBloodmoney
On Thu, Jul 7, 2011 at 12:04 AM, Venkatraman S wrote: > I am doing some bechmarking on the performance of both the versions of the > query: > 1. > Item.objects.filter(created_by__employees__org__in=u.employees_set.all().values_list('org')) > and > 2. Items.objects.extra(where=['created_by_id in (s

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Venkatraman S
I am doing some bechmarking on the performance of both the versions of the query: 1. Item.objects.filter(created_by__employees__org__in=u.employees_set.all().values_list('org')) and 2. Items.objects.extra(where=['created_by_id in (select e.user_id from myapp_employees e, myapp_organization o where

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Venkatraman S
On Wed, Jul 6, 2011 at 8:09 PM, Marc Aymerich wrote: > I swear this time it will work! > > Item.objects.filter(created_by__employees__org__in=u.employees_set.all().values_list('org')) > > I cried on seeing this ;) Thanks a tonne. Guess this will go into the django hall of fame! But i have a Q :

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Marc Aymerich
On Wed, Jul 6, 2011 at 2:46 PM, Venkatraman S wrote: > > > On Wed, Jul 6, 2011 at 2:10 PM, Marc Aymerich wrote: >> >> This one should work: >> Items.objects.filters(created_by__employee__org=A.org) > > Nope. Emp has a FK for User, not the other way round. I swear this time it will work! Item.obj

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Tom Evans
On Wed, Jul 6, 2011 at 2:40 PM, akaariai wrote: > Maybe this? > > user = current_user > user_org_employees = Employees.objects.filter(org=user.org) > user_org_items = > Items.objects.filter(created_by__in=user_org_employees) > No, users can belong to multiple organizations, so no using user.org.

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread akaariai
On Jul 5, 10:06 pm, Venkatraman S wrote: > I tried asking around in IRC, and stumbled on a few possible solutions, > would be great if someone shed some more light: > > I have the following models: > > class Organization(models.Model): >   name                = models.CharField(max_length=100, bla

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Venkatraman S
On Wed, Jul 6, 2011 at 2:10 PM, Marc Aymerich wrote: > This one should work: > Items.objects.filters(created_by__employee__org=A.org) > Nope. Emp has a FK for User, not the other way round. -- You received this message because you are subscribed to the Google Groups "Django users" group. To p

Re: Complex Query help - get items from the org to which the user belongs

2011-07-06 Thread Marc Aymerich
On Wed, Jul 6, 2011 at 4:46 AM, Venkatraman S wrote: > > On Wed, Jul 6, 2011 at 2:26 AM, Marc Aymerich wrote: > >> Say there are 3 Users in an org : A, B and C with each creating 3,4,5 >> items respectively, and 'A' is the current user; then i need a query which >> returns all these items(i.e, 1

Re: Complex Query help - get items from the org to which the user belongs

2011-07-05 Thread Venkatraman S
On Wed, Jul 6, 2011 at 8:39 AM, Venkatraman S wrote: > > On Wed, Jul 6, 2011 at 12:36 AM, Venkatraman S wrote: > >> I tried asking around in IRC, and stumbled on a few possible solutions, >> would be great if someone shed some more light: >> >> I have the following models: >> >> class Organizati

Re: Complex Query help - get items from the org to which the user belongs

2011-07-05 Thread Venkatraman S
On Wed, Jul 6, 2011 at 12:36 AM, Venkatraman S wrote: > I tried asking around in IRC, and stumbled on a few possible solutions, > would be great if someone shed some more light: > > I have the following models: > > class Organization(models.Model): > name= models.CharField(max_l

Re: Complex Query help - get items from the org to which the user belongs

2011-07-05 Thread Venkatraman S
On Wed, Jul 6, 2011 at 2:26 AM, Marc Aymerich wrote: > Say there are 3 Users in an org : A, B and C with each creating 3,4,5 items > respectively, and 'A' is the current user; then i need a query which returns > all these items(i.e, 12 items) when i supply A.. > > Item.objects.filter(employees__o

Re: Complex Query help - get items from the org to which the user belongs

2011-07-05 Thread Marc Aymerich
On Tue, Jul 5, 2011 at 9:06 PM, Venkatraman S wrote: > I tried asking around in IRC, and stumbled on a few possible solutions, > would be great if someone shed some more light: > > I have the following models: > > class Organization(models.Model): > name= models.CharField(max_le

Complex Query help - get items from the org to which the user belongs

2011-07-05 Thread Venkatraman S
I tried asking around in IRC, and stumbled on a few possible solutions, would be great if someone shed some more light: I have the following models: class Organization(models.Model): name= models.CharField(max_length=100, blank=False) class Employees(models.Model): org

Spatial SQL to Django Query Help

2011-02-01 Thread Ryan Clark
Hi there - I have two related models. MapUnitPolys has a 1:M relationship to DescriptionOfMapUnits. MapUnitPolys has a geometry field, DescriptionOfMapUnits does not. The query that I'm trying to accomplish is something like SELECT DISTINCT DescriptionOfMapUnits.* FROM ( MapUnitPolys INNER JO

Re: Query help

2010-11-02 Thread Itai Tavor
On 03/11/2010, at 2:10 AM, Prashanth wrote: > > > On Tue, Nov 2, 2010 at 9:35 AM, Itai Tavor wrote: > Hi people, > > Given this: > > class Category(models.Model): >name = models.CharField() >parent = models.ForeignKey('self', blank=True, null=True) >tree_path = models.CharField() #

Re: Query help

2010-11-02 Thread Prashanth
On Tue, Nov 2, 2010 at 9:35 AM, Itai Tavor wrote: > Hi people, > > Given this: > > class Category(models.Model): >name = models.CharField() >parent = models.ForeignKey('self', blank=True, null=True) >tree_path = models.CharField() # //.../// > > class Item(models.Model): >name = m

Query help

2010-11-02 Thread Itai Tavor
Hi people, Given this: class Category(models.Model): name = models.CharField() parent = models.ForeignKey('self', blank=True, null=True) tree_path = models.CharField() # //.../// class Item(models.Model): name = models.CharField() categories = models.ManyToManyField(Category)

Re: Query Help: "Get Blogs where last Entry.title='foo'"

2009-06-16 Thread Jason
Took me awhile to wrap my head around it, but that got me where I need to be, thank you. On Jun 15, 3:41 pm, Streamweaver wrote: > A custom method in the model could work fine. > > If you set a 'get_latest_by' in the Entry Model > ->http://www.djangoproject.com/documentation/models/get_latest/

Re: Query Help: "Get Blogs where last Entry.title='foo'"

2009-06-15 Thread Streamweaver
A custom method in the model could work fine. If you set a 'get_latest_by' in the Entry Model -> http://www.djangoproject.com/documentation/models/get_latest/ Then in the Blog Model define something like: def last_is_foo(self): if Entry.objects.latest().title == 'foo' return True

Re: Query Help: "Get Blogs where last Entry.title='foo'"

2009-06-15 Thread Jason
Close. That returns all entrys that have title foo. I need all blogs *where the last entry* has title foo. I think I need to leverage .extra(where=something) but not having much luck. On Jun 15, 2:36 pm, Streamweaver wrote: > If I understand your question right there is an example that cover

Re: Query Help: "Get Blogs where last Entry.title='foo'"

2009-06-15 Thread Streamweaver
If I understand your question right there is an example that covers this exact situation that may help at http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships I believe it could come out to something like this: e = Entry.objects.filter(title__exact='foo') blo

Query Help: "Get Blogs where last Entry.title='foo'"

2009-06-15 Thread Jason
I'm new to django and attempting to leverage the query functionality. I'm not sure if what I'm attempting requires custom sql or if I"m just missing something. I'm spending alot of time with the docs but haven't found guidance for this issue. Just using blogs as an easy example, assume the model

Re: query help (distinct)

2009-06-05 Thread eric.frederich
Thats good, but now I need to turn those id's back into content types. >From there I need to get the model's verbose plural name also. The idea is a view where the types of assets are listed, Rooms, Projectors, etc. BTW, if you couldn't tell from the sql statement before, this is a booking appli

Re: query help (distinct)

2009-06-05 Thread Daniel Roseman
On Jun 5, 6:56 pm, "eric.frederich" wrote: > How can I get distinct content_types from a model like this?... > > In sqlite I did the following and it worked... > sqlite> select distinct content_type_id from booking_managedasset; > > class ManagedAsset(models.Model): >     content_type   = models.

query help (distinct)

2009-06-05 Thread eric.frederich
How can I get distinct content_types from a model like this?... In sqlite I did the following and it worked... sqlite> select distinct content_type_id from booking_managedasset; class ManagedAsset(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.Posi

Re: database query help

2006-05-03 Thread Dave St.Germain
oops.  My mistake.  I should've looked up the db-api docs before replying...On 5/3/06, Adrian Holovaty <[EMAIL PROTECTED] > wrote:On 5/3/06, tsnyder <[EMAIL PROTECTED] > wrote:> I think I have missed something in the documentation, and I am hoping someone> can help me.  What is the django equivalen

Re: database query help

2006-05-03 Thread Adrian Holovaty
On 5/3/06, tsnyder <[EMAIL PROTECTED]> wrote: > I think I have missed something in the documentation, and I am hoping someone > can help me. What is the django equivalent of doing something like this: > > select * from event where semester_id = 1 and category_id = 2 > > Where the event table is r

Re: database query help

2006-05-03 Thread Dave St.Germain
I think this is what you're looking for:Event.objects.filter(semester__id=1,category__id=2)(note the two underscores)On 5/3/06, tsnyder < [EMAIL PROTECTED]> wrote:Hi all,I think I have missed something in the documentation, and I am hoping someone can help me.  What is the django equivalent of doin

database query help

2006-05-03 Thread tsnyder
Hi all, I think I have missed something in the documentation, and I am hoping someone can help me. What is the django equivalent of doing something like this: select * from event where semester_id = 1 and category_id = 2 Where the event table is related to two other tables (semester and categ