Re: querying for an empty set in m-r?
I vaguely remember an advice to use __isnull in a similar situation. Maybe one of these discussions turns up something useful: http://simon.bofh.ms/logger/django/search/?q=isnull --~--~-~--~~~---~--~~ 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: querying for an empty set in m-r?
On 4/12/06, Max Battcher <[EMAIL PROTECTED]> wrote: > Oh, right. You could do: > > tags = [tag for tag in Tag.objects.all() if tag.article_set.count() == 0] > > n queries for n tags, but it is "pretty" Python. Doing db-cleanup > shouldn't happen all that often, so you probably don't need the best > performance, and I'd just stick with the list comprehension. > > Too bad there isn't some cool easy Python way to make a list > comprehension/generators like that as Lazy as the Descriptors... There is, but it's only in Python 2.4: >>> g = (x**2 for x in range(10)) >>> g >>> g.next() 0 >>> g.next() 1 >>> g.next() 4 Check out http://www.python.org/dev/peps/pep-0289/ (Generator Expressions) -- Ian Clelland <[EMAIL PROTECTED]> --~--~-~--~~~---~--~~ 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: querying for an empty set in m-r?
Russell, Just wanted to thank you for responding to my post. You did interpret my question correctly, though I was obviously hoping to avoid using raw SQL. Anyway, thanks again. Brian On Apr 12, 2006, at 1:10 AM, Russell Cloran wrote: > > Hi, > > On Tue, 2006-04-11 at 21:20 -0400, Max Battcher wrote: >> if sometag.article_set.count() == 0: >> pass # your code here > > This will test if one tag has an empty article_set. AFAICT, the > question > is on how to find all tags which have an empty article_set. Yes, you > could do it this way, but it means querying the DB again for every > tag. > >>> Each Article can be associated with multiple Tags and vice versa. >>> How can I query the Tag model to obtain all Tags with no associated >>> articles? (m-r) I basically want to delete unused tags. > > Unfortunately, it seems that you have to drop to SQL. See the thread > started by me on the subject yesterday, > > http://groups.google.com/group/django-users/browse_thread/thread/ > e708abca0baf6a32/15ae0b421f607983 > > Russell > > > > --~--~-~--~~~---~--~~ 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: querying for an empty set in m-r?
Max, Just wanted to thank you for responding to my question. You are correct that the list comprehension is pythonic, though I was ideally looking for a efficient method from a database perspective. Thanks, Brian On Apr 12, 2006, at 2:16 AM, Max Battcher wrote: > > Russell Cloran wrote: >> Hi, >> >> On Tue, 2006-04-11 at 21:20 -0400, Max Battcher wrote: >>> if sometag.article_set.count() == 0: >>> pass # your code here >> >> This will test if one tag has an empty article_set. AFAICT, the >> question >> is on how to find all tags which have an empty article_set. Yes, you >> could do it this way, but it means querying the DB again for every >> tag. > > Oh, right. You could do: > > tags = [tag for tag in Tag.objects.all() if tag.article_set.count() > == 0] > > n queries for n tags, but it is "pretty" Python. Doing db-cleanup > shouldn't happen all that often, so you probably don't need the best > performance, and I'd just stick with the list comprehension. > > Too bad there isn't some cool easy Python way to make a list > comprehension/generators like that as Lazy as the Descriptors... > Byte-code parsing, anyone? > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442447 > > -- > --Max Battcher-- > http://www.worldmaker.net/ > "I'm gonna win, trust in me / I have come to save this world / and in > the end I'll get the grrrl!" --Machinae Supremacy, Hero (Promo Track) > > > --~--~-~--~~~---~--~~ 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: querying for an empty set in m-r?
Russell Cloran wrote: > Hi, > > On Tue, 2006-04-11 at 21:20 -0400, Max Battcher wrote: >> if sometag.article_set.count() == 0: >> pass # your code here > > This will test if one tag has an empty article_set. AFAICT, the question > is on how to find all tags which have an empty article_set. Yes, you > could do it this way, but it means querying the DB again for every tag. Oh, right. You could do: tags = [tag for tag in Tag.objects.all() if tag.article_set.count() == 0] n queries for n tags, but it is "pretty" Python. Doing db-cleanup shouldn't happen all that often, so you probably don't need the best performance, and I'd just stick with the list comprehension. Too bad there isn't some cool easy Python way to make a list comprehension/generators like that as Lazy as the Descriptors... Byte-code parsing, anyone? http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442447 -- --Max Battcher-- http://www.worldmaker.net/ "I'm gonna win, trust in me / I have come to save this world / and in the end I'll get the grrrl!" --Machinae Supremacy, Hero (Promo Track) --~--~-~--~~~---~--~~ 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: querying for an empty set in m-r?
if sometag.article_set.count() == 0: pass # your code here On 4/11/06, Brian Elliott <[EMAIL PROTECTED]> wrote: > > Suppose I have the following two model objects: > > class Tag(models.Model): > name = models.CharField(maxlength=10) > > class Article(models.Model): > tags = models.ManyToManyField(Tag) > > > Each Article can be associated with multiple Tags and vice versa. > How can I query the Tag model to obtain all Tags with no associated > articles? (m-r) I basically want to delete unused tags. > > Thanks, > Brian > > > > > -- --Max Battcher-- http://www.worldmaker.net/ All progress is based upon a universal innate desire on the part of every organism to live beyond its income. --Samuel Butler --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
querying for an empty set in m-r?
Suppose I have the following two model objects: class Tag(models.Model): name = models.CharField(maxlength=10) class Article(models.Model): tags = models.ManyToManyField(Tag) Each Article can be associated with multiple Tags and vice versa. How can I query the Tag model to obtain all Tags with no associated articles? (m-r) I basically want to delete unused tags. Thanks, Brian --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---