Re: testing foo_set existence for each object in a queryset
Hi Lior, Thanks for the effort - but I'm not sure it can be done this way. The problem is the related model is not accessible at the level of MyModel - only at the level of its instances (if I'm saying that correctly). So if I try something like that I get the error message: "Cannot resolve keyword 'foo_set' into field. Choices are: ..." You did give me an idea though... MyModel.objects.filter(foo__endtime__gt = datetime.datetime.now()).distinct() This seems to work. Dammit - there goes my motivation to learn about managers. :) Actually - that's the same as "SELECT DISTINCT" in sql - so is perhaps not very efficient... That should provide a bit of a motivation boost at least. Thanks for the help. On Feb 20, 5:00 pm, Lior Sion wrote: > Dan, > > If I understand your question correctly, you are struggling with > creating the filtering you wrote in your message on the queryset level > (without going to the db for each object), right? > > Hard to say without actually seeing your code and testing, but would > this be the same? > > MyModel.objects.filter(foo_set__endtime__gt == > datetime.datetime.now()) > > I don't think you'll need the exists, as only existing objects will > come back from the query. > > On Feb 20, 3:50 am, Dan wrote: > > > > > > > > > Hi, > > > Long time lurker - first time poster - hopefully future answerer... > > > Basically what I want to do can be done with: > > > result = [w for w in MyModel.objects.all() if > > w.foo_set.filter(endtime__gt = datetime.datetime.now()).exists()] > > > Is there anyway to do this using the queryset api? > > > Hopefully it's not too stupid a question... -- 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: testing foo_set existence for each object in a queryset
Thanks Shawn That's helpful. I was actually looking at the manager documentation today thinking perhaps that was what I needed - but couldn't quite wrap my head around it. But knowing that it indeed is the way to go will no doubt provide the motivation I need. (I have problems learning anything if I'm not sure it solves a problem I have). On Feb 20, 4:12 pm, Shawn Milochik wrote: > Not only is it not a stupid question, but it's one of the best > possible types of questions. Any time someone comes in and makes it > obvious that they've thought about their problem and made an attempt > to solve it themselves, they get my respect. > > The easiest answer to your question is to make a custom manager (a > subclass of models.Manager) for your model. > > http://docs.djangoproject.com/en/1.2/topics/db/managers/ > > You can add your logic to an override of the get() of filter(), or add > an entirely new method, such as pending() or ready_to_send(). > > If your data grows to the point where this becomes unwieldy, you could > speed things up by using signals, so that instances of the model > represented by 'foo' in your example would update a field in your main > model when they are created, changed, or deleted. This would allow you > to use metadata in your main model instead of having to do the extra > joins on every database read. > > Shawn -- 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: testing foo_set existence for each object in a queryset
Dan, If I understand your question correctly, you are struggling with creating the filtering you wrote in your message on the queryset level (without going to the db for each object), right? Hard to say without actually seeing your code and testing, but would this be the same? MyModel.objects.filter(foo_set__endtime__gt == datetime.datetime.now()) I don't think you'll need the exists, as only existing objects will come back from the query. On Feb 20, 3:50 am, Dan wrote: > Hi, > > Long time lurker - first time poster - hopefully future answerer... > > Basically what I want to do can be done with: > > result = [w for w in MyModel.objects.all() if > w.foo_set.filter(endtime__gt = datetime.datetime.now()).exists()] > > Is there anyway to do this using the queryset api? > > Hopefully it's not too stupid a question... -- 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: testing foo_set existence for each object in a queryset
Not only is it not a stupid question, but it's one of the best possible types of questions. Any time someone comes in and makes it obvious that they've thought about their problem and made an attempt to solve it themselves, they get my respect. The easiest answer to your question is to make a custom manager (a subclass of models.Manager) for your model. http://docs.djangoproject.com/en/1.2/topics/db/managers/ You can add your logic to an override of the get() of filter(), or add an entirely new method, such as pending() or ready_to_send(). If your data grows to the point where this becomes unwieldy, you could speed things up by using signals, so that instances of the model represented by 'foo' in your example would update a field in your main model when they are created, changed, or deleted. This would allow you to use metadata in your main model instead of having to do the extra joins on every database read. Shawn -- 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.