Re: Query question
You should use 'F' expression from django.db.models So your expression would be like: ProductSale.objects.all().filter(date_expires__lt = now().date).exclude( date_purchased__gt = F('date_expires')) Which roughly translates to SELECT * FROM productsale WHERE date_expires < CURRENT_DATE AND NOT (date_purchased > date_expires) in mysql. On Tuesday, 18 August 2015 06:26:56 UTC+5:30, Lee Hinde wrote: > > Given this model, I want to find all ProductSale objects that have expired > and where the Person doesn't have a later (unexpired) purchase of the same > product. > > class ProductSale(models.Model): > product = models.ForeignKey(Product) > person = models.ForeignKey(Person) > ... > date_expires = models.DateField(blank=True, db_index=True) > > > The closest I've gotten is in the shell where I get a values_list of > person and product for expired and unexpired and then compare the two > lists. That leaves me with a list of tuples, that (due to the size of the > list, I think) I can't use in a query.extra. > > Any suggestions would be appreciated. > > (and sorry for the lame subject. I couldn't think of a way to be both > succinct and informative.) > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/26d0c805-54e5-4d9f-b3cb-07955cce9a66%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Query question
You should use 'F' expression from django.db.models So your expression would be like: ProductSale.objects.all().filter(date_expires__lg = now()).exclude(date_purchased__gt = F('date_expires')) On Tuesday, 18 August 2015 06:26:56 UTC+5:30, Lee Hinde wrote: > > Given this model, I want to find all ProductSale objects that have expired > and where the Person doesn't have a later (unexpired) purchase of the same > product. > > class ProductSale(models.Model): > product = models.ForeignKey(Product) > person = models.ForeignKey(Person) > ... > date_expires = models.DateField(blank=True, db_index=True) > > > The closest I've gotten is in the shell where I get a values_list of > person and product for expired and unexpired and then compare the two > lists. That leaves me with a list of tuples, that (due to the size of the > list, I think) I can't use in a query.extra. > > Any suggestions would be appreciated. > > (and sorry for the lame subject. I couldn't think of a way to be both > succinct and informative.) > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/daa1c2a5-1c8d-41f4-b4ca-73d0f6c70a1f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Query question
Given this model, I want to find all ProductSale objects that have expired and where the Person doesn't have a later (unexpired) purchase of the same product. class ProductSale(models.Model): product = models.ForeignKey(Product) person = models.ForeignKey(Person) ... date_expires = models.DateField(blank=True, db_index=True) The closest I've gotten is in the shell where I get a values_list of person and product for expired and unexpired and then compare the two lists. That leaves me with a list of tuples, that (due to the size of the list, I think) I can't use in a query.extra. Any suggestions would be appreciated. (and sorry for the lame subject. I couldn't think of a way to be both succinct and informative.) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BePoMw3z0TwbHUOLwVKg26N5Bc1q6BQ014hsNVCiws1r083nA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: ORM Query question
Ok, thanks. On Monday, 13 July 2015 16:14:29 UTC+1, Avraham Serour wrote: > > Personally I don't see obvious errors, but it is hard to tell with only > this small snippet. > > If it works what are you worried about? Performance? If you are having > performance problems you can try caching the query > > On Mon, Jul 13, 2015 at 6:08 PM, Joss Ingram <joss@gmail.com > > wrote: > >> Ok, thanks fair enough. >> >> my Question then is : >> >> Is the query above bad? >> >> On Mon, Jul 13, 2015 at 4:00 PM, Avraham Serour <tov...@gmail.com >> > wrote: >> >>> You told a story but didn't ask a question >>> >>> On Mon, Jul 13, 2015 at 5:54 PM, Joss Ingram <joss@gmail.com >>> > wrote: >>> >>>> I'm not getting much joy with my question, is there too little info? >>>> >>>> On Friday, 10 July 2015 15:31:50 UTC+1, Joss Ingram wrote: >>>>> >>>>> Hi, >>>>> >>>>> In my models I have an event index page type, which can have child >>>>> event pages, each event page can be tagged (using taggit), and I want to >>>>> produce a list of distinct tags used in the events that belong to that >>>>> event index on the index itself. I'm using the Django CMS Wagtail, but I >>>>> guess this is really a Django ORM query question. I''ve got some code as >>>>> part of my model which is working but I don't think it's the most >>>>> efficient >>>>> way of doing this. Still trying unlearn doing things with SQL and learn >>>>> how >>>>> the same thing should be done in Django. >>>>> >>>>> My code as part of the event index class is >>>>> >>>>> @property >>>>> def event_tag_list(self): >>>>> child_events = EventPage.objects.live().descendant_of(self) >>>>> return >>>>> Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') >>>>> >>>>> Any feedback would be appreciated. >>>>> >>>>> Thanks >>>>> >>>>> Joss >>>>> >>>>> >>>>> >>>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Django users" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to django-users...@googlegroups.com . >>>> To post to this group, send email to django...@googlegroups.com >>>> . >>>> Visit this group at http://groups.google.com/group/django-users. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com?utm_medium=email_source=footer> >>>> . >>>> >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "Django users" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/django-users/qB8W-I3jJWg/unsubscribe. >>> To unsubscribe from this group and all its topics, send an email to >>> django-users...@googlegroups.com . >>> To post to this group, send email to django...@googlegroups.com >>> . >>> Visit this group at http://groups.google.com/group/django-users. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com >>> >>> <https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com?utm_medium=email_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com . >> To pos
Re: ORM Query question
Personally I don't see obvious errors, but it is hard to tell with only this small snippet. If it works what are you worried about? Performance? If you are having performance problems you can try caching the query On Mon, Jul 13, 2015 at 6:08 PM, Joss Ingram <joss.ing...@gmail.com> wrote: > Ok, thanks fair enough. > > my Question then is : > > Is the query above bad? > > On Mon, Jul 13, 2015 at 4:00 PM, Avraham Serour <tovm...@gmail.com> wrote: > >> You told a story but didn't ask a question >> >> On Mon, Jul 13, 2015 at 5:54 PM, Joss Ingram <joss.ing...@gmail.com> >> wrote: >> >>> I'm not getting much joy with my question, is there too little info? >>> >>> On Friday, 10 July 2015 15:31:50 UTC+1, Joss Ingram wrote: >>>> >>>> Hi, >>>> >>>> In my models I have an event index page type, which can have child >>>> event pages, each event page can be tagged (using taggit), and I want to >>>> produce a list of distinct tags used in the events that belong to that >>>> event index on the index itself. I'm using the Django CMS Wagtail, but I >>>> guess this is really a Django ORM query question. I''ve got some code as >>>> part of my model which is working but I don't think it's the most efficient >>>> way of doing this. Still trying unlearn doing things with SQL and learn how >>>> the same thing should be done in Django. >>>> >>>> My code as part of the event index class is >>>> >>>> @property >>>> def event_tag_list(self): >>>> child_events = EventPage.objects.live().descendant_of(self) >>>> return >>>> Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') >>>> >>>> Any feedback would be appreciated. >>>> >>>> Thanks >>>> >>>> Joss >>>> >>>> >>>> >>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Django users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to django-users+unsubscr...@googlegroups.com. >>> To post to this group, send email to django-users@googlegroups.com. >>> Visit this group at http://groups.google.com/group/django-users. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com >>> <https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com?utm_medium=email_source=footer> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "Django users" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/django-users/qB8W-I3jJWg/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com?utm_medium=email_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAA%3DX%3DqeGwJ1OwOQw0C4jAr7nzWJUsKGeryAQ%3DgoicuJbCWr5HQ%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAA%3DX%3DqeGwJ1OwOQw0C4jAr7nzWJUsKGeryAQ%3DgoicuJbCWr5HQ%40mail.gmail.com?utm_medium=email_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFWa6tJKaa08QFDgsdRQdqaL-T4UT3XvLSFkh%2BAmSSP6VW774w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: ORM Query question
Ok, thanks fair enough. my Question then is : Is the query above bad? On Mon, Jul 13, 2015 at 4:00 PM, Avraham Serour <tovm...@gmail.com> wrote: > You told a story but didn't ask a question > > On Mon, Jul 13, 2015 at 5:54 PM, Joss Ingram <joss.ing...@gmail.com> > wrote: > >> I'm not getting much joy with my question, is there too little info? >> >> On Friday, 10 July 2015 15:31:50 UTC+1, Joss Ingram wrote: >>> >>> Hi, >>> >>> In my models I have an event index page type, which can have child event >>> pages, each event page can be tagged (using taggit), and I want to produce >>> a list of distinct tags used in the events that belong to that event index >>> on the index itself. I'm using the Django CMS Wagtail, but I guess this is >>> really a Django ORM query question. I''ve got some code as part of my model >>> which is working but I don't think it's the most efficient way of doing >>> this. Still trying unlearn doing things with SQL and learn how the same >>> thing should be done in Django. >>> >>> My code as part of the event index class is >>> >>> @property >>> def event_tag_list(self): >>> child_events = EventPage.objects.live().descendant_of(self) >>> return >>> Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') >>> >>> Any feedback would be appreciated. >>> >>> Thanks >>> >>> Joss >>> >>> >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com >> <https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com?utm_medium=email_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to a topic in the > Google Groups "Django users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/django-users/qB8W-I3jJWg/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com?utm_medium=email_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3DX%3DqeGwJ1OwOQw0C4jAr7nzWJUsKGeryAQ%3DgoicuJbCWr5HQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: ORM Query question
You told a story but didn't ask a question On Mon, Jul 13, 2015 at 5:54 PM, Joss Ingram <joss.ing...@gmail.com> wrote: > I'm not getting much joy with my question, is there too little info? > > On Friday, 10 July 2015 15:31:50 UTC+1, Joss Ingram wrote: >> >> Hi, >> >> In my models I have an event index page type, which can have child event >> pages, each event page can be tagged (using taggit), and I want to produce >> a list of distinct tags used in the events that belong to that event index >> on the index itself. I'm using the Django CMS Wagtail, but I guess this is >> really a Django ORM query question. I''ve got some code as part of my model >> which is working but I don't think it's the most efficient way of doing >> this. Still trying unlearn doing things with SQL and learn how the same >> thing should be done in Django. >> >> My code as part of the event index class is >> >> @property >> def event_tag_list(self): >> child_events = EventPage.objects.live().descendant_of(self) >> return >> Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') >> >> Any feedback would be appreciated. >> >> Thanks >> >> Joss >> >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com?utm_medium=email_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFWa6t%2B1dpT%3D3oYG%2BqRopr9vWWXwY573%2BM%2BzkuT8ijxwYXo%2Brg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: ORM Query question
I'm not getting much joy with my question, is there too little info? On Friday, 10 July 2015 15:31:50 UTC+1, Joss Ingram wrote: > > Hi, > > In my models I have an event index page type, which can have child event > pages, each event page can be tagged (using taggit), and I want to produce > a list of distinct tags used in the events that belong to that event index > on the index itself. I'm using the Django CMS Wagtail, but I guess this is > really a Django ORM query question. I''ve got some code as part of my model > which is working but I don't think it's the most efficient way of doing > this. Still trying unlearn doing things with SQL and learn how the same > thing should be done in Django. > > My code as part of the event index class is > > @property > def event_tag_list(self): > child_events = EventPage.objects.live().descendant_of(self) > return > Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') > > Any feedback would be appreciated. > > Thanks > > Joss > > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61cf7c97-ae40-4c71-a094-ff1d819a7ce5%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
ORM Query question
Hi, In my models I have an event index page type, which can have child event pages, each event page can be tagged (using taggit), and I want to produce a list of distinct tags used in the events that belong to that event index on the index itself. I'm using the Django CMS Wagtail, but I guess this is really a Django ORM query question. I''ve got some code as part of my model which is working but I don't think it's the most efficient way of doing this. Still trying unlearn doing things with SQL and learn how the same thing should be done in Django. My code as part of the event index class is @property def event_tag_list(self): child_events = EventPage.objects.live().descendant_of(self) return Tag.objects.filter(mysite_eventpagetag_items__isnull=False).filter(mysite_eventpagetag_items__content_object_id=child_events).order_by('slug').distinct('slug') Any feedback would be appreciated. Thanks Joss -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e819245a-7885-4d56-9f9b-4bfac665f011%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Referencing Django List Objects From Query Question
How do I call django objects from a query for example licenses = Licenses.objects.all() how I get my licenses, then im trying to loop through licenses for every object from licenses that exist in a post save something to vc. if insert == 'yes': insert_data = request.POST.copy() license_tag='' for lis in licenses: license_tag += insert_data[lis('Licenses')] + ',' the data returns like [, , , , , , , , , , , , , , , , , , , , '...(remaining elements truncated)...'] I get the error 'Licenses' object is not callable I've tried insert_data[lis('Licenses')] insert_data[lis['Licenses']] insert_data[lis.Licenses] any ideas? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/09cf61fb-584f-4a8f-b932-09d084f0fbe8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: A query question
:-) ModelB.objects.filter(~Q(modelA__fielddate__range(date1,date2))) On Tue, Feb 26, 2013 at 10:53 AM, ozgur yilmazwrote: > Thanks for the replies. I should use exclude, to find A objects, so > should try this i think: > > ModelB.objects.exclude(modelA__fielddate__range(date1,date2)) > > 2013/2/26 Ronan Foucher : > > ModelB.objects.filter(modelA__fielddate__range(date1,date2)) > > > > Based on the db backend and the number of column you can add an index on > > fielddate ( check explain output to see if it's useful since it depends > on > > db backend/ dbengine/structure of the table to make it 'inexpensive' ) > > > > ~ Ronan > > > > > > On Tue, Feb 26, 2013 at 9:59 AM, ozgur yilmaz wrote: > >> > >> Hi, > >> > >> Actually my question is not about the date range. i'm planning to use > >> __lte and __gte filters. My problem is to solve the query (getting A > >> objects using B objects) inexpensively. Thanks anyway, > >> > >> I used: > >> > >> b_objects = B.objects.filter( activity_date__gte = specific_start_date > >> , activity_date__lte = specific_end_date ) > >> > >> a_objects = A.objects.exclude( kisi__in = b_objects ) > >> > >> Is it enough? > >> > >> 2013/2/26 carlos : > >> > Hi, maybe use DateField__range(date1,date2) > >> > > >> > Cheers > >> > > >> > > >> > On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmaz > wrote: > >> >> > >> >> Hi all, > >> >> > >> >> I have to build a query, if possible an inexpensive query: > >> >> > >> >> Model A: > >> >> ... > >> >> ... > >> >> > >> >> Model B: > >> >> ForeignKey( Model A ) > >> >> Date > >> >> ... > >> >> > >> >> Model B is an activity with a date field. I want to find which Model > A > >> >> objects didnt join an activity between specific dates. > >> >> > >> >> What are the appropriate ways to find this result? Any ideas? > >> >> > >> >> Thanks, > >> >> > >> >> -- > >> >> You received this message because you are subscribed to the Google > >> >> Groups > >> >> "Django users" group. > >> >> To unsubscribe from this group and stop receiving emails from it, > send > >> >> an > >> >> email to django-users+unsubscr...@googlegroups.com. > >> >> To post to this group, send email to django-users@googlegroups.com. > >> >> Visit this group at > http://groups.google.com/group/django-users?hl=en. > >> >> For more options, visit https://groups.google.com/groups/opt_out. > >> >> > >> >> > >> > > >> > -- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "Django users" group. > >> > To unsubscribe from this group and stop receiving emails from it, send > >> > an > >> > email to django-users+unsubscr...@googlegroups.com. > >> > To post to this group, send email to django-users@googlegroups.com. > >> > Visit this group at http://groups.google.com/group/django-users?hl=en > . > >> > For more options, visit https://groups.google.com/groups/opt_out. > >> > > >> > > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Django users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to django-users+unsubscr...@googlegroups.com. > >> To post to this group, send email to django-users@googlegroups.com. > >> Visit this group at http://groups.google.com/group/django-users?hl=en. > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > > > > > -- > > Cordialement / Best Regards > > > > Ronan FOUCHER > > IS / IT > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users+unsubscr...@googlegroups.com. > > To post to this group, send email to django-users@googlegroups.com. > > Visit this group at http://groups.google.com/group/django-users?hl=en. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Cordialement / Best Regards Ronan FOUCHER IS / IT -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: A query question
Thanks for the replies. I should use exclude, to find A objects, so should try this i think: ModelB.objects.exclude(modelA__fielddate__range(date1,date2)) 2013/2/26 Ronan Foucher: > ModelB.objects.filter(modelA__fielddate__range(date1,date2)) > > Based on the db backend and the number of column you can add an index on > fielddate ( check explain output to see if it's useful since it depends on > db backend/ dbengine/structure of the table to make it 'inexpensive' ) > > ~ Ronan > > > On Tue, Feb 26, 2013 at 9:59 AM, ozgur yilmaz wrote: >> >> Hi, >> >> Actually my question is not about the date range. i'm planning to use >> __lte and __gte filters. My problem is to solve the query (getting A >> objects using B objects) inexpensively. Thanks anyway, >> >> I used: >> >> b_objects = B.objects.filter( activity_date__gte = specific_start_date >> , activity_date__lte = specific_end_date ) >> >> a_objects = A.objects.exclude( kisi__in = b_objects ) >> >> Is it enough? >> >> 2013/2/26 carlos : >> > Hi, maybe use DateField__range(date1,date2) >> > >> > Cheers >> > >> > >> > On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmaz wrote: >> >> >> >> Hi all, >> >> >> >> I have to build a query, if possible an inexpensive query: >> >> >> >> Model A: >> >> ... >> >> ... >> >> >> >> Model B: >> >> ForeignKey( Model A ) >> >> Date >> >> ... >> >> >> >> Model B is an activity with a date field. I want to find which Model A >> >> objects didnt join an activity between specific dates. >> >> >> >> What are the appropriate ways to find this result? Any ideas? >> >> >> >> Thanks, >> >> >> >> -- >> >> You received this message because you are subscribed to the Google >> >> Groups >> >> "Django users" group. >> >> To unsubscribe from this group and stop receiving emails from it, send >> >> an >> >> email to django-users+unsubscr...@googlegroups.com. >> >> To post to this group, send email to django-users@googlegroups.com. >> >> Visit this group at http://groups.google.com/group/django-users?hl=en. >> >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> >> >> > >> > -- >> > You received this message because you are subscribed to the Google >> > Groups >> > "Django users" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> > an >> > email to django-users+unsubscr...@googlegroups.com. >> > To post to this group, send email to django-users@googlegroups.com. >> > Visit this group at http://groups.google.com/group/django-users?hl=en. >> > For more options, visit https://groups.google.com/groups/opt_out. >> > >> > >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > > > > -- > Cordialement / Best Regards > > Ronan FOUCHER > IS / IT > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: A query question
ModelB.objects.filter(modelA__fielddate__range(date1,date2)) Based on the db backend and the number of column you can add an index on fielddate ( check explain output to see if it's useful since it depends on db backend/ dbengine/structure of the table to make it 'inexpensive' ) ~ Ronan On Tue, Feb 26, 2013 at 9:59 AM, ozgur yilmazwrote: > Hi, > > Actually my question is not about the date range. i'm planning to use > __lte and __gte filters. My problem is to solve the query (getting A > objects using B objects) inexpensively. Thanks anyway, > > I used: > > b_objects = B.objects.filter( activity_date__gte = specific_start_date > , activity_date__lte = specific_end_date ) > > a_objects = A.objects.exclude( kisi__in = b_objects ) > > Is it enough? > > 2013/2/26 carlos : > > Hi, maybe use DateField__range(date1,date2) > > > > Cheers > > > > > > On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmaz wrote: > >> > >> Hi all, > >> > >> I have to build a query, if possible an inexpensive query: > >> > >> Model A: > >> ... > >> ... > >> > >> Model B: > >> ForeignKey( Model A ) > >> Date > >> ... > >> > >> Model B is an activity with a date field. I want to find which Model A > >> objects didnt join an activity between specific dates. > >> > >> What are the appropriate ways to find this result? Any ideas? > >> > >> Thanks, > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Django users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to django-users+unsubscr...@googlegroups.com. > >> To post to this group, send email to django-users@googlegroups.com. > >> Visit this group at http://groups.google.com/group/django-users?hl=en. > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users+unsubscr...@googlegroups.com. > > To post to this group, send email to django-users@googlegroups.com. > > Visit this group at http://groups.google.com/group/django-users?hl=en. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Cordialement / Best Regards Ronan FOUCHER IS / IT -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: A query question
yes for me but if you want see the query you try use django-debug-toolbar and you see how long it takes the query Cheers On Tue, Feb 26, 2013 at 11:59 AM, ozgur yilmazwrote: > Hi, > > Actually my question is not about the date range. i'm planning to use > __lte and __gte filters. My problem is to solve the query (getting A > objects using B objects) inexpensively. Thanks anyway, > > I used: > > b_objects = B.objects.filter( activity_date__gte = specific_start_date > , activity_date__lte = specific_end_date ) > > a_objects = A.objects.exclude( kisi__in = b_objects ) > > Is it enough? > > 2013/2/26 carlos : > > Hi, maybe use DateField__range(date1,date2) > > > > Cheers > > > > > > On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmaz wrote: > >> > >> Hi all, > >> > >> I have to build a query, if possible an inexpensive query: > >> > >> Model A: > >> ... > >> ... > >> > >> Model B: > >> ForeignKey( Model A ) > >> Date > >> ... > >> > >> Model B is an activity with a date field. I want to find which Model A > >> objects didnt join an activity between specific dates. > >> > >> What are the appropriate ways to find this result? Any ideas? > >> > >> Thanks, > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Django users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to django-users+unsubscr...@googlegroups.com. > >> To post to this group, send email to django-users@googlegroups.com. > >> Visit this group at http://groups.google.com/group/django-users?hl=en. > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users+unsubscr...@googlegroups.com. > > To post to this group, send email to django-users@googlegroups.com. > > Visit this group at http://groups.google.com/group/django-users?hl=en. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: A query question
Hi, Actually my question is not about the date range. i'm planning to use __lte and __gte filters. My problem is to solve the query (getting A objects using B objects) inexpensively. Thanks anyway, I used: b_objects = B.objects.filter( activity_date__gte = specific_start_date , activity_date__lte = specific_end_date ) a_objects = A.objects.exclude( kisi__in = b_objects ) Is it enough? 2013/2/26 carlos: > Hi, maybe use DateField__range(date1,date2) > > Cheers > > > On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmaz wrote: >> >> Hi all, >> >> I have to build a query, if possible an inexpensive query: >> >> Model A: >> ... >> ... >> >> Model B: >> ForeignKey( Model A ) >> Date >> ... >> >> Model B is an activity with a date field. I want to find which Model A >> objects didnt join an activity between specific dates. >> >> What are the appropriate ways to find this result? Any ideas? >> >> Thanks, >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: A query question
Hi, maybe use DateField__range(date1,date2) Cheers On Tue, Feb 26, 2013 at 5:43 AM, ozgur yilmazwrote: > Hi all, > > I have to build a query, if possible an inexpensive query: > > Model A: > ... > ... > > Model B: > ForeignKey( Model A ) > Date > ... > > Model B is an activity with a date field. I want to find which Model A > objects didnt join an activity between specific dates. > > What are the appropriate ways to find this result? Any ideas? > > Thanks, > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
A query question
Hi all, I have to build a query, if possible an inexpensive query: Model A: ... ... Model B: ForeignKey( Model A ) Date ... Model B is an activity with a date field. I want to find which Model A objects didnt join an activity between specific dates. What are the appropriate ways to find this result? Any ideas? Thanks, -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: ORM Query question
Or, using range: MyModel.objects.filter( Q(a__range=(1,5)) | Q(b__range=(20,70)) ) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ORM Query question
http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects MyModel.objects.filter( ( Q(a__gt=1) & Q(a__lt=5) ) | ( Q(b__gt=20) & Q(b__lt=70) ) ) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ORM Query question
On 13 April 2010 01:09, zwebwrote: > how do i do this kind of condition in django orm filter: > > ( 1 < a < 5) or ( 20 < b < 70) > > select * from table where (a between 1 and 5) or (b between 20 and 70) > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > http://docs.djangoproject.com/en/dev/ref/models/querysets/#range -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
ORM Query question
how do i do this kind of condition in django orm filter: ( 1 < a < 5) or ( 20 < b < 70) select * from table where (a between 1 and 5) or (b between 20 and 70) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: A Query question about Many-to-many relationships
Hi Atamert, Thank you so much! The following statement works! This is really simple but fantastic!!! Publication.objects.filter(article__in = article_qs).annotate(Count('article')) Sincerely, Jason -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: A Query question about Many-to-many relationships
Hi Jason, On Monday 22 February 2010 09:57:18 Jason wrote: > If I already had a QuerySet of Article named articles, how to get a > QuerySet of Publication by these articles, You can use field lookup operator `in` like this: >>> Publication.objects.filter( >>> article_set__id__in = article_qs.values_list('id', flat=True)) Maybe the ORM even accepts article_set__in = article_qs, but I haven't tried. > and count the articles by > the Publication? >>> article_qs.annotate(Count('publications')) See http://docs.djangoproject.com/en/dev/ref/models/querysets/ Hope this helps. -- Saygılarımla, Atamert Ölçgen -+- --+ +++ www.muhuk.com mu...@jabber.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
A Query question about Many-to-many relationships
Dear All, Model source code: from django.db import models class Publication(models.Model): title = models.CharField(max_length=30) class Article(models.Model): headline = models.CharField(max_length=100) publications = models.ManyToManyField(Publication) If I already had a QuerySet of Article named articles, how to get a QuerySet of Publication by these articles, and count the articles by the Publication? Thanks a lot! Jason -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Complex Query Question
mystand wrote: > I need to do a query that selects all Events that a logged in user can edit. > They can edit an event if they are in the default group, if they are in any > of the other groups, or they are one of the users added to the event. first of all, it might be easier if you follow the database convention of naming your tables in singular. in this case, your events model should be called 'Event' and not 'Events'. that makes it more readable when you use singular in ForeignKey fields and plural in ManyToManyField's in your case, the general strategy is to use Q() objects. the idea is to define each possibility separately and then join them all with the '|' operator. something like this (untested): @login_required def editable_groups(request): user = request.user evs_by_defgroup = Q(default_group__in=user.group_set) evs_by_groups = Q(group__in=user.group_set) evs_by_users = user.events_set myevents = Events.objects.filter (evs_by_defgroup | evs_by_groups | evs_by_users) return render_to_response ("template.html", {'myevents':myevents}) (i'm not sure about '|'ing the Q() objects with the queryset 'user.events_set', but i don't know how to express this as a Q() object) -- Javier -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Complex Query Question
Hi all, I have a question about a complicated query I need to do. I have a model for an event like this: class Events(models.Model): default_group = models.ForeignKey(Group, related_name='default_group', null=True) group = models.ManyToManyField(Group) users = models.ManyToManyField(User) title = models.CharField(max_length=255) shortname = models.SlugField(max_length=25, unique=True) plus some more fields that are not relevant to this question. This is for a event registration site. The default group must be chosen from the event creator's groups. group can be any group and users can be any users. I need to do a query that selects all Events that a logged in user can edit. They can edit an event if they are in the default group, if they are in any of the other groups, or they are one of the users added to the event. I've read the docs and am still missing something. How can I do this query as elegantly as possible? Thanks, A Django Newbie. -- View this message in context: http://old.nabble.com/Complex-Query-Question-tp26451658p26451658.html Sent from the django-users mailing list archive at Nabble.com. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=.
Re: Foreign Key query question
On Apr 11, 12:32 pm, Malcolm Tredinnickwrote: > That's definitely a small bug, then. I've opened ticket #10790 so that > it gets fixed eventually. Since it's not a functionality bug (the answer > is still correct), it will be fixed after 1.1 now, but we will fix it. I > understand why it's occurring (the comparison to NULL is a very special > case in this situation). Thanks Malcolm! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On Sat, 2009-04-11 at 01:02 -0700, nikita kozlovsky wrote: > On Apr 11, 3:11 am, Malcolm Tredinnick> wrote: > > > > Django's SQL is going exactly what you suspect and not using any outer > > join here. Using a simplified version of the original two models: > > > > class Student(models.Model): > >... > > > > class Message(models.Model): > >title = models.CharField(max_length=50) > >student = models.ForeignKey(Student) > > > > ... the SQL that is generated for Message.objects.filter(student=None) > > is: > > > > SELECT `outer_message`.`id`, `outer_message`.`title`, > > `outer_message`.`student_id` FROM `outer_message` WHERE > > `outer_message`.`student_id` IS NULL > > Yes, this correctly if student = models.ForeignKey(Student). > I use this simplified models with student = models.ForeignKey > (Student, null=True), sql that is generated for Message.objects.filter > (student=None) is: > > SELECT "messages_message"."id", "messages_message"."title", > "messages_message"."student_id" FROM "messages_message" LEFT OUTER > JOIN "messages_student" ON ("messages_message"."student_id" = > "messages_student"."id") WHERE "messages_student"."id" IS NULL That's definitely a small bug, then. I've opened ticket #10790 so that it gets fixed eventually. Since it's not a functionality bug (the answer is still correct), it will be fixed after 1.1 now, but we will fix it. I understand why it's occurring (the comparison to NULL is a very special case in this situation). Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On Apr 11, 3:11 am, Malcolm Tredinnickwrote: > Django's SQL is going exactly what you suspect and not using any outer > join here. Using a simplified version of the original two models: > > class Student(models.Model): > ... > > class Message(models.Model): > title = models.CharField(max_length=50) > student = models.ForeignKey(Student) > > ... the SQL that is generated for Message.objects.filter(student=None) > is: > > SELECT `outer_message`.`id`, `outer_message`.`title`, > `outer_message`.`student_id` FROM `outer_message` WHERE > `outer_message`.`student_id` IS NULL Yes, this correctly if student = models.ForeignKey(Student). I use this simplified models with student = models.ForeignKey (Student, null=True), sql that is generated for Message.objects.filter (student=None) is: SELECT "messages_message"."id", "messages_message"."title", "messages_message"."student_id" FROM "messages_message" LEFT OUTER JOIN "messages_student" ON ("messages_message"."student_id" = "messages_student"."id") WHERE "messages_student"."id" IS NULL i use django svn-10510 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On Fri, 2009-04-10 at 05:45 -0700, nikita kozlovsky wrote: > On Mar 9, 3:21 am, Malcolm Tredinnick> wrote: > > Hello, Malcolm. > > > > Again, the correct syntax would be: > > > Message.objects.filter(student__isnull=True) > > Why ORM uses LEFT OUTER JOIN on this query ? Why not "... WHERE > student_id IS NULL" ? Django's SQL is going exactly what you suspect and not using any outer join here. Using a simplified version of the original two models: class Student(models.Model): ... class Message(models.Model): title = models.CharField(max_length=50) student = models.ForeignKey(Student) ... the SQL that is generated for Message.objects.filter(student=None) is: SELECT `outer_message`.`id`, `outer_message`.`title`, `outer_message`.`student_id` FROM `outer_message` WHERE `outer_message`.`student_id` IS NULL (I have put it into an application called "outer", hence the table prefix and I happen to be using MySQL in my directory of test applications at the moment, hence the backticks instead of normal single quotes.) The ORM tries to trim any unnecessary table joins, particularly towards the end of the join sequence. Any times that it includes unnecessary tables, or uses outer joins when we can reliably see it should be an inner join is pretty much a bug. Here, for example, it knows that the student_id field is a reliable indicator of whether there is a related object, so it does exactly what you expect. Sometimes people see extra table joins that they aren't expecting because they are ordering by a field in the related model. For example, using Meta.ordering = ("student",) here would require an extra join. But, the simplest won't need more than the message table. Does that answer your question? Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On Mar 9, 3:21 am, Malcolm Tredinnickwrote: Hello, Malcolm. > > Again, the correct syntax would be: > > Message.objects.filter(student__isnull=True) Why ORM uses LEFT OUTER JOIN on this query ? Why not "... WHERE student_id IS NULL" ? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
Take it as given that I agree with the bulk of Daniel's reply. It's correct. I have one quibble, however... On Sun, 2009-03-08 at 10:54 -0700, Daniel Roseman wrote: [...] > What do you mean by 'student=1'? Do you mean 'the student whose pk is > 1'? If so, the correct filter syntax for this on its own is: > Message.objects.filter(student_id=1) > or > Message.objects.filter(student__pk=1) Not quite. Message.objects.filter(student=1) is also valid. It's automatically converted to filter(student__pk=1). > > And, it doesn't make any sense to have student=None as a filter. > Again, the correct syntax would be: > Message.objects.filter(student__isnull=True) Again, not really. Message.objects.filter(student=None) is exactly equivalent to filter(student__isnull=True). That's been true since well before 1.0 was released (it was introduced into trunk at the time queryset-refactor merged). See http://docs.djangoproject.com/en/dev/ref/models/querysets/#exact (student=None is parsed as student__exact=None and we document how exact comparisons with None behave). Your alternative versions aren't wrong, but the original poster's versions aren't either. I didn't want people running around thinking things that worked were unintentional side-effects or anything like that. Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On 8 Mrz., 18:54, Daniel Rosemanwrote: > On Mar 8, 3:50 pm, Christoph Wegscheider > > > > wrote: > > Hi, > > I have the following model: > > class Message(models.Model): > > student = models.ForeignKey(Student, blank=True, null=True) > > message = models.CharField(max_length=200) > > > I want to filter for: > > message_list = Message.objects.filter(student=1) > > OR > > message_list = Message.objects.filter(student=None) > > > mesage_list = Message.objects.fileter(student__in=[1,None]) > > is not working > > > I want something like > > SELECT * FROM message WHERE pk = 1 OR pk IS NULL > > in django syntax. is that possible? > > But student is not the primary key - it's a foreign key to another > table. Your model and description don't match your SQL at all. > > What do you mean by 'student=1'? Do you mean 'the student whose pk is > 1'? If so, the correct filter syntax for this on its own is: > Message.objects.filter(student_id=1) > or > Message.objects.filter(student__pk=1) > > And, it doesn't make any sense to have student=None as a filter. > Again, the correct syntax would be: > Message.objects.filter(student__isnull=True) > > So, I'm assuming your actual requirement for 'all messages which > belong to the student whose ID is 1, or which don't belong to any > student at all'. Is that the case? If so, you want something like > this: > Message.objects.filter(Q(student__isnull=true) | (Q(student_id=1)) > > -- > DR. Hi Daniel, of course you are right the SQL is wrong (right one: SELECT * FROM message WHERE student_id = 1 OR student_id IS NULL). The 'Q thing' was exactly I was looking for. Thanks, Christoph --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Foreign Key query question
On Mar 8, 3:50 pm, Christoph Wegscheiderwrote: > Hi, > I have the following model: > class Message(models.Model): > student = models.ForeignKey(Student, blank=True, null=True) > message = models.CharField(max_length=200) > > I want to filter for: > message_list = Message.objects.filter(student=1) > OR > message_list = Message.objects.filter(student=None) > > mesage_list = Message.objects.fileter(student__in=[1,None]) > is not working > > I want something like > SELECT * FROM message WHERE pk = 1 OR pk IS NULL > in django syntax. is that possible? But student is not the primary key - it's a foreign key to another table. Your model and description don't match your SQL at all. What do you mean by 'student=1'? Do you mean 'the student whose pk is 1'? If so, the correct filter syntax for this on its own is: Message.objects.filter(student_id=1) or Message.objects.filter(student__pk=1) And, it doesn't make any sense to have student=None as a filter. Again, the correct syntax would be: Message.objects.filter(student__isnull=True) So, I'm assuming your actual requirement for 'all messages which belong to the student whose ID is 1, or which don't belong to any student at all'. Is that the case? If so, you want something like this: Message.objects.filter(Q(student__isnull=true) | (Q(student_id=1)) -- DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Foreign Key query question
Hi, I have the following model: class Message(models.Model): student = models.ForeignKey(Student, blank=True, null=True) message = models.CharField(max_length=200) I want to filter for: message_list = Message.objects.filter(student=1) OR message_list = Message.objects.filter(student=None) mesage_list = Message.objects.fileter(student__in=[1,None]) is not working I want something like SELECT * FROM message WHERE pk = 1 OR pk IS NULL in django syntax. is that possible? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: django query question
On Fri, Feb 27, 2009 at 2:40 PM, Bobby Robertswrote: > > hi all. I have a situation where I have two tables, auctions and > bids. I need to do a subquery as such: > > select distinct id from auctions_auction where Active=1 and id IN > (select distinct AuctionId_id from auctions_bid where BidderId=6) > > > how would I do this in django? > > > Without knowing your table structure I'd say it's something like: Auction.objects.values_list('id', flat=True).filter(active=1).filter(id__in=Bid.objects.filter(bidder_id=6).values('auction_id')) on svn or Auction.objects.values_list('id', flat=True).filter(active=1).filter(id__in=Bid.objects.filter(bidder_id=6).values('auction_id').query) on 1.0 -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
django query question
hi all. I have a situation where I have two tables, auctions and bids. I need to do a subquery as such: select distinct id from auctions_auction where Active=1 and id IN (select distinct AuctionId_id from auctions_bid where BidderId=6) how would I do this in django? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Q query question
> I'm experimenting (so far without success) with a bunch of ways to try > to turn a chain into a queryset. If anybody has any tips, I will be > grateful. Ok, got it, I think - using the idiom from this snippet: http://www.djangosnippets.org/snippets/26/ (My problem was using the chain to populate the choices for a ChoiceField.) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Q query question
On Apr 23, 9:00 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > Complex Q-object combinations on trunk do have a few problems (bugs). It > turns out to be quite hard to get all the combinations working > correctly. This is one of the areas that development of the > queryset-refactor branch has devoted a lot of time to fixing, so things > will improve in trunk very soon (or you can try out your code on the > branch). Thanks, Malcolm. I want some features from queryset_refactor and some from newforms_admin, but none of them urgently (yet), so I think I'll work around the bug and wait. Looks like this is working in *most* cases (see below), and maybe simpler anyway: part1 = InventoryItem.objects.filter(product=self, onhand__gt=0) part2 = InventoryItem.objects.filter(product=self, inventory_date__range=(weekstart, thisdate), planned__gt=0, onhand__exact=0, received__exact=0) return itertools.chain(part1, part2) The itertools.chain works fine in situations where I just iterate thru the items. But in one case, I really need to return a queryset. (Tried turning the chain into a plain list, that did not work.) I'm experimenting (so far without success) with a bunch of ways to try to turn a chain into a queryset. If anybody has any tips, I will be grateful. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Q query question
On Wed, 2008-04-23 at 06:03 -0700, bobhaugen wrote: > Replying to myself with more clues: > > I see the problem: > > For this filter statement: > items = InventoryItem.objects.filter( > Q(inventory_date__range=(weekstart, thisdate), received__exact=0) > | > Q(onhand__gt=0)) > > ,,, the SQL generated looks like this: > SELECT > "orders_inventoryitem"."id","orders_inventoryitem"."producer_id","orders_inventoryitem"."product_id","orders_inventoryitem"."inventory_date","orders_inventoryitem"."planned","orders_inventoryitem"."received","orders_inventoryitem"."onhand" > FROM "orders_inventoryitem" WHERE > (("orders_inventoryitem"."inventory_date" BETWEEN 2008-04-21 AND > 2008-04-23 OR "orders_inventoryitem"."received" = 0.00 OR > "orders_inventoryitem"."onhand" > 0.00)) ORDER BY > "orders_inventoryitem"."product_id" ASC, > "orders_inventoryitem"."producer_id" ASC, > "orders_inventoryitem"."inventory_date" ASC'} > > So the SQL ORs all of the Q clauses, and does not separate the Q > clause that I thought should have been ANDed. Complex Q-object combinations on trunk do have a few problems (bugs). It turns out to be quite hard to get all the combinations working correctly. This is one of the areas that development of the queryset-refactor branch has devoted a lot of time to fixing, so things will improve in trunk very soon (or you can try out your code on the branch). Without spending too much time on it, though, it looks like your intuition about how Q-objects should work is pretty much correct and the SQL construction is just not behaving itself. Regards, Malcolm -- Borrow from a pessimist - they don't expect it back. http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Q query question
Replying to myself with more clues: I see the problem: For this filter statement: items = InventoryItem.objects.filter( Q(inventory_date__range=(weekstart, thisdate), received__exact=0) | Q(onhand__gt=0)) ,,, the SQL generated looks like this: SELECT "orders_inventoryitem"."id","orders_inventoryitem"."producer_id","orders_inventoryitem"."product_id","orders_inventoryitem"."inventory_date","orders_inventoryitem"."planned","orders_inventoryitem"."received","orders_inventoryitem"."onhand" FROM "orders_inventoryitem" WHERE (("orders_inventoryitem"."inventory_date" BETWEEN 2008-04-21 AND 2008-04-23 OR "orders_inventoryitem"."received" = 0.00 OR "orders_inventoryitem"."onhand" > 0.00)) ORDER BY "orders_inventoryitem"."product_id" ASC, "orders_inventoryitem"."producer_id" ASC, "orders_inventoryitem"."inventory_date" ASC'} So the SQL ORs all of the Q clauses, and does not separate the Q clause that I thought should have been ANDed. On the other hand, this single Q query properly generates ANDed clauses: items = InventoryItem.objects.filter( Q(inventory_date__range=(weekstart, thisdate), received__exact=0)) The SQL: SELECT "orders_inventoryitem"."id","orders_inventoryitem"."producer_id","orders_inventoryitem"."product_id","orders_inventoryitem"."inventory_date","orders_inventoryitem"."planned","orders_inventoryitem"."received","orders_inventoryitem"."onhand" FROM "orders_inventoryitem" WHERE ("orders_inventoryitem"."inventory_date" BETWEEN 2008-04-21 AND 2008-04-23 AND "orders_inventoryitem"."received" = 0.00) ORDER BY "orders_inventoryitem"."product_id" ASC, "orders_inventoryitem"."producer_id" ASC, "orders_inventoryitem"."inventory_date" ASC'} So is the first generated SQL a bug? Or am I still missing something? (And still hunting for a better (or any clean) way to accomplish my goal...) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Q query question
I'm trying to retrieve all Inventory Items whose onhand quantity is greater than zero, OR (whose date is within a specified range AND whose received quantity is zero). InventoryItems have three relevant fields for this query: onhand, inventory_date, and received. I currently have only 2 inventory items, only one of which matches the desired criteria. If I specify the following two separate queries, I get the correct results from each query: 1. items = InventoryItem.objects.filter(Q(onhand__gt=0) ) ...correctly returns an empty list (currenly no items have onhand quantities). 2. items = InventoryItem.objects.filter( Q(inventory_date__range=(weekstart, thisdate), received__exact=0)) ...correctly returns a list containing one item. If I put the clauses together like this: items = InventoryItem.objects.filter( Q(onhand__gt=0) | Q(inventory_date__range=(weekstart, thisdate), received__exact=0)) ...I do not get the expected result (one item), but rather 2 items, including one that violates both clauses. But if I put these clauses together like this (omitting the date range): items = InventoryItem.objects.filter( Q(onhand__gt=0) | Q(received__exact=0)) ...I get the correct result. So I assume I do not understand something about either this clause: Q(inventory_date__range=(weekstart, thisdate), received__exact=0) or how to combine it into a larger set of filter clauses with an OR. I thought it was ANDing the first part with the last part, and that is how it seems to act on its own. But it behaves differently when combined in a larger filter with an OR. So what am I missing? Or, is there a better way to accomplish my goal? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Query question
I have an agreement model that can be "amended" using a relationship to itself. I need to get a queryset of the active agreements for a certain time period but the following query does not work i believe because of the way the Django orm handles joins. Can this query be expressed using the Django orm? return self.exclude(Q(date_effective__gt=dtuntil) | Q(date_expires__lt=dtstart) | Q(date_terminated__isnull=False)) \ .filter(Q(amendment__isnull=True) | Q(amendment__date_effective__gt=dtstart)) - Justin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: second query question
> class Property(models.Model): > > thing = models.ForeignKey(Thing) > property_type = models.ForeignKey(PropertyType) > value = models.CharField(...) > ... > > I want to find, for a given PropertyType, all the Things that are > lacking any Property of that type. > > Like my previous question, in SQL I could do it with an EXCEPT: > > SELECT id > FROM myapp_thing > EXCEPT > SELECT thing_id > FROM myapp_property > WHERE property_type_id = ... > > > How can I best do these sorts of EXCEPT queries with the database API? As Maciej mentioned previously, the easiest way is to use the .extra() call that the DB API provides: things = Thing.objects.extra(where=[""" id NOT IN ( SELECT thing_id FROM myapp_property WHERE property_type_id = %s ) """], params=[42] ) Both Maciej's SQL and the above SQL should be fairly portable as long as you're not porting to some older braindead version of MySQL (pre 4.1, IIRC) that lacks support for subselects [mutters under breath about that *brilliant* decision: "gee, let's omit a hugely useful functionality from ANSI SQL..." I'm not sure I'd even consider pre-5.0 MySQL a "real" database] Some backends prefer "EXISTS" to "IN" (I noticed that for certain cases, using EXISTS in PostgreSQL triggered a monumental speed-up from minutes to seconds) which just takes a minor bit of SQL tweaking to something like where=[""" NOT EXISTS ( SELECT 1 FROM myapp_property WHERE thing_id = myapp_thing.id AND property_type_id = %s ) """] (adjust the name of "myapp_thing.id" to be whatever that fieldname is aliased to) YMMV. -tim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: second query question
I believe it would be: Thing.objects.all().exclude(property__property_type__exact = type).distinct() -rfd On Aug 28, 12:24 pm, James Tauber <[EMAIL PROTECTED]> wrote: > There's another query I want to do that I can't seem to wrap my head > around at the moment: > > Say I have a model where Things have Properties each of a particular > PropertyType. So > > class Property(models.Model): > > thing = models.ForeignKey(Thing) > property_type = models.ForeignKey(PropertyType) > value = models.CharField(...) > ... > > I want to find, for a given PropertyType, all the Things that are > lacking any Property of that type. > > Like my previous question, in SQL I could do it with an EXCEPT: > > SELECT id > FROM myapp_thing > EXCEPT > SELECT thing_id > FROM myapp_property > WHERE property_type_id = ... > > How can I best do these sorts of EXCEPT queries with the database API? > > James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
second query question
There's another query I want to do that I can't seem to wrap my head around at the moment: Say I have a model where Things have Properties each of a particular PropertyType. So class Property(models.Model): thing = models.ForeignKey(Thing) property_type = models.ForeignKey(PropertyType) value = models.CharField(...) ... I want to find, for a given PropertyType, all the Things that are lacking any Property of that type. Like my previous question, in SQL I could do it with an EXCEPT: SELECT id FROM myapp_thing EXCEPT SELECT thing_id FROM myapp_property WHERE property_type_id = ... How can I best do these sorts of EXCEPT queries with the database API? James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
RESOLVED: Re: Query Question ...
Took some custom SQL, but this works well: content = Content.objects \ .filter(approved__isnull = False) \ .extra(where=['id not in (SELECT content_id FROM relationship WHERE user_id = %d and "hasRead" = TRUE)' % user.id]) \ .order_by('-created') Phew. Enjoy, Sandy --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Query Question ...
Hi all (again), I'm hoping someone can help me with the syntax for this django query I have three related tables class User(models.Model): # stuff ... class Content(models.Model): # stuff ... class Relationship(models.Model): user = models.ForeignKey(User) content = models.ForeignKey(Content) hasRead = models.BooleanField() I want to get all the Content that the User has NOT read. The catch: The Relationship record between the User and the Content may not exist. It only exists if a piece of content is actually read. If they haven't, the record doesn't exist. If I try: content = Content.objects.filter(relationship_user = user, relationship__hasRead = False) I don't get any records. So I try to get crafty: content = Content.objects.filter( Q(relationship__isnull = False) | Q(relationship__hasRead = False)).filter(relationship_user = user) has the same problem. Looking at the raw SQL you can see the problem: FROM "content" INNER JOIN "relationship" ON "content"."id" = "content__relationship"."content_id" WHERE "content__relationship"."id" IS NULL OR "content__relationship"."hasRead" = False --and-- FROM "content" INNER JOIN ... WHERE "content__relationship"."content_id" IS NULL OR "relationship"."hasRead" = False well, I'm sure you see the problem. Perhaps what I need is a subquery, but I'm not sure. Can I phrase such a query with the django ORM? Any suggestions? Thanks Sandy --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Quick Query Question
On 5/15/07, Collin Anderson <[EMAIL PROTECTED]> wrote: > class Laptop(models.Model): > def laptops_out_on(date): > 'get a list of laptops out on a given day' > result = Laptop.objects.none() > for x in Rental.objects.filter(checkout__lte=date, > checkin__gte=date): > result = result | x.laptops.all() > return result > laptops_out_on = staticmethod(laptops_out_on) Why not get the list of active rentals (1 query) then get the list of laptops associated with those rentals? Also, make it a manager method rather than a static method. class LaptopManager(models.Manager): def checked_out(self, date=None): if date is None: import datetime date = datetime.date.today() active_rental_ids = [rental['id'] for rental in Rental.objects.filter(checkout__gte=date, checkin_lte=date).values('id')] qs = super(LaptopManager, self).get_query_set(). return qs.filter(rental__id__in=active_rental_ids) class Laptops(models.Model): objects = LaptopManager() #get all checked out laptops today: Laptops.objects.checked_out() ...Code not tested since I can't sandbox a project and DB quickly just now. More: http://www.djangoproject.com/documentation/db-api/#lookups-that-span-relationships --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Quick Query Question
I am working with a laptop rental program, and I was wondering if there is a better way to do this lookup. This works fine, but it seems like it could be better. from django.db import models class Laptop(models.Model): def laptops_out_on(date): 'get a list of laptops out on a given day' result = Laptop.objects.none() for x in Rental.objects.filter(checkout__lte=date, checkin__gte=date): result = result | x.laptops.all() return result laptops_out_on = staticmethod(laptops_out_on) class Rental(models.Model): checkout = models.DateField(blank=True) checkin = models.DateField(blank=True) laptops = models.ManyToManyField(Laptop, blank=True) Thanks, Collin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Re: Database query question
On 9/18/06, Tom Smith <[EMAIL PROTECTED]> wrote: > Not really.. having built and manipulated (with exludes, filters, order > etc.) I'd quite like to peep at the SQL before it gets executed to see if it > makes sense I think it probably is possible by screwing around with internal variables of the QuerySet class, provided you know what those variables are, but the easier way, if you're just testing, is mentioned here (it shows you the query after the fact, but throws in the execution time as a bonus): http://www.djangoproject.com/documentation/faq/#how-can-i-see-the-raw-sql-queries-django-is-running -- "May the forces of evil become confused on the way to your house." -- George Carlin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
On 18 Sep 2006, at 16:26, James Bennett wrote:p.exclude() returns a new QuerySet with the filtering, so you need to grab the return value, like this: if len(notsitelist)>=1: print "excluding sites: ", notsitelist p = p.exclude(fk_site__in=notsitelist) That was it... silly me, just missed it trying to keep the code simple...Thanks!! tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
On 18 Sep 2006, at 16:27, Michael Radziej wrote: Is there way to do p.get_query(.)p.show_sql() I really don't get what this is supposed to mean, it just looks like seriously broken syntax. Not really.. having built and manipulated (with exludes, filters, order etc.) I'd quite like to peep at the SQL before it gets executed to see if it makes senseregards and thankstom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Re: Database query question
On 9/18/06, Tom Smith <[EMAIL PROTECTED]> wrote: > I have... > > p = Product.objects > if len(notsitelist)>=1: > print "exluding sites: ", notsitelist > p.exclude(fk_site__in=notsitelist) p.exclude() returns a new QuerySet with the filtering, so you need to grab the return value, like this: if len(notsitelist)>=1: print "excluding sites: ", notsitelist p = p.exclude(fk_site__in=notsitelist) -- "May the forces of evil become confused on the way to your house." -- George Carlin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
Tom Smith wrote: > Thanks... I'm not sure but I don't think this is working > > I have... > > p = Product.objects > if len(notsitelist)>=1: > print "exluding sites: ", notsitelist > p.exclude(fk_site__in=notsitelist) > > if len(cats)>=1: > print "exluding categories: ", cats > p.exclude(catalog__in=cats) > > p.order_by('?') > return p.select_related()[:50] > > ... and funnily the order is the same every time (not random) and > although the print statements are printing, it doesn't seem to be > effecting the results. > All the QuerySet methods that affect the SQL generation don't change the QuerySet but return a modified QuerySet. You want to do p = p.exclude(...) etc. > Is there way to do > > p.get_query(.)p.show_sql() I really don't get what this is supposed to mean, it just looks like seriously broken syntax. Michael --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
> Query sets are not executed on the database until you iterate, or > otherwise try to extract data from them. > > o = Recipe.objects > o2 = o.filter(xx) > o3 = o2.filter(yy) > o4 = o3.filter(zz) > > print o4 > > will result in just 1 query getting issued to the database - the final > query with three filters. Thanks... I'm not sure but I don't think this is working I have... p = Product.objects if len(notsitelist)>=1: print "exluding sites: ", notsitelist p.exclude(fk_site__in=notsitelist) if len(cats)>=1: print "exluding categories: ", cats p.exclude(catalog__in=cats) p.order_by('?') return p.select_related()[:50] ... and funnily the order is the same every time (not random) and although the print statements are printing, it doesn't seem to be effecting the results. Is there way to do p.get_query(.)p.show_sql() ? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Re: Database query question
On 9/16/06, Tom Smith <[EMAIL PROTECTED]> wrote: > > Recipe.objects.exclude(category__in=words) > > ...which is great, but now having set words to and empty list [] I > get the error... This is an issue that has been previously reported: http://code.djangoproject.com/ticket/2473 > So... would it be possible to do this? > > ...or would the first line go off and get all the objects (I have > about a million records)? Query sets are not executed on the database until you iterate, or otherwise try to extract data from them. o = Recipe.objects o2 = o.filter(xx) o3 = o2.filter(yy) o4 = o3.filter(zz) print o4 will result in just 1 query getting issued to the database - the final query with three filters. Yours, Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
On 16 Sep 2006, at 12:42, Malcolm Tredinnick wrote: > > On Sat, 2006-09-16 at 12:25 +0100, Tom Smith wrote: >> How would I build an "AND" query... >> >> for example... >> >> words = ['chicken', 'beef', 'lamb'] >> Recipe.objects.exclude(title__contains=words) >> >> I don't want to do 3 queries... > > Please start a new thread for new topics, rather than dropping into > the > middle of an existing thread like this. > > To answer your question: have a look at > http://www.djangoproject.com/documentation/db_api/#complex-lookups- > with-q-objects Thanks... That documentation has allowed me to use the __in method... like this... Recipe.objects.exclude(category__in=words) ...which is great, but now having set words to and empty list [] I get the error... (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))) AND (NOT (`myproject_product`.`category` IN (' at line 1") So... would it be possible to do this? o = Recipe.objects if len(words) > 1: o.exclude(category__in=words) if len(someotherlist) > 1: o.exclude(someotherfield__in= someotherlist) o.execute() ...or would the first line go off and get all the objects (I have about a million records)? Thanks tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: Database query question
On Sat, 2006-09-16 at 12:25 +0100, Tom Smith wrote: > How would I build an "AND" query... > > for example... > > words = ['chicken', 'beef', 'lamb'] > Recipe.objects.exclude(title__contains=words) > > I don't want to do 3 queries... Please start a new thread for new topics, rather than dropping into the middle of an existing thread like this. To answer your question: have a look at http://www.djangoproject.com/documentation/db_api/#complex-lookups-with-q-objects Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Database query question
How would I build an "AND" query... for example... words = ['chicken', 'beef', 'lamb'] Recipe.objects.exclude(title__contains=words) I don't want to do 3 queries... thanks tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---