Re: How to make such query with django ORM?
2007/11/30, RajeshD <[EMAIL PROTECTED]>: > > > As others have recommended, just use the extra() method. It's not so > bad :) > Hi. What about if I want to do the same thing but in the admin interface? Let's say for example, that in the Article form, I just want to show the Blogs that have certain title. Is it posible to do it? I should use the extra() method in some place? Thanks. Mauro. --~--~-~--~~~---~--~~ 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: How to make such query with django ORM?
> > I need to get all blogs that belong to certain user and are empty (do > not have any articles). I can't figure out how to make it with without > extra() method. As others have recommended, just use the extra() method. It's not so bad :) If you really really want to avoid that, you could add a count integer field to your Blog model and maintain its value from overridden methods Article.save() and Article.delete(). So Blog.count would get refreshed every time you add/update/delete articles and your desired query becomes a straight lookup on 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: How to make such query with django ORM?
Actually, the QuerySet objects are lazy. This is, there's no such query until you access the data. Or so do I believe. El jue, 29-11-2007 a las 01:23 -0800, SmileyChris escribi�: > On Nov 28, 9:08 am, Eratothene <[EMAIL PROTECTED]> wrote: > > I need to get all blogs that belong to certain user and are empty (do > > not have any articles). I can't figure out how to make it with without > > extra() method. > > Since you can't do aggregate methods in the ORM yet, you'll just have > to use extra and do a sub-select to count the articles linked to each. > > --~--~-~--~~~---~--~~ 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: How to make such query with django ORM?
On Nov 28, 9:08 am, Eratothene <[EMAIL PROTECTED]> wrote: > I need to get all blogs that belong to certain user and are empty (do > not have any articles). I can't figure out how to make it with without > extra() method. Since you can't do aggregate methods in the ORM yet, you'll just have to use extra and do a sub-select to count the articles linked to each. --~--~-~--~~~---~--~~ 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: How to make such query with django ORM?
> Here's a solution that should give you a list (not a QuerySet) of all > the blogs. Don't know if this is best practice though. You can get all > blogs of a user with: > user.blog_set.all() > From this QuerySet you want only these without articles: > blogs = [] > for blog in user.blog_set.all(): >#if the blog has no articles >if blog.article_set.all() == Article.objects.none(): > blogs.append(blog) > > Hope it works ;) It does work without a doubt. But I am looking for mor efficient solution that would not require an extra query for every blog. I need to make this with a single query. --~--~-~--~~~---~--~~ 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: How to make such query with django ORM?
> I need to get all blogs that belong to certain user and are empty (do > not have any articles). I can't figure out how to make it with without > extra() method. Here's a solution that should give you a list (not a QuerySet) of all the blogs. Don't know if this is best practice though. You can get all blogs of a user with: user.blog_set.all() >From this QuerySet you want only these without articles: blogs = [] for blog in user.blog_set.all(): #if the blog has no articles if blog.article_set.all() == Article.objects.none(): blogs.append(blog) Hope it works ;) --~--~-~--~~~---~--~~ 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: How to make such query with django ORM?
On 11/27/07, Eratothene ([EMAIL PROTECTED]) wrote: > >Hi django users! > >I have two simple models: Blog and Article. >class Blog(models.Model): >title = models.CharField() >content = models.TextField() >user = models.ForeignKey(User) > >class Article(models.Model): >title = models.CharField() >content = models.TextField() >blog = models.ForeignKey(Blog) > > >I need to get all blogs that belong to certain user and are empty (do >not have any articles). I can't figure out how to make it with without >extra() method. > >Any ideas? I'm still pretty new at this myself but I don't see any other answers yet so I figure a little info is better than naught. I'm sure others will join in with better descriptions than mine. Depending upon context, I think you'll need to either use the select_related() or _set() i.e. article.select_related() or article.blog_set() with a filter something like .filter(blog__content__isnull=True) Review the db docs with the above to get a _much_ better description of how they work and in which context. HTH Scott --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
How to make such query with django ORM?
Hi django users! I have two simple models: Blog and Article. class Blog(models.Model): title = models.CharField() content = models.TextField() user = models.ForeignKey(User) class Article(models.Model): title = models.CharField() content = models.TextField() blog = models.ForeignKey(Blog) I need to get all blogs that belong to certain user and are empty (do not have any articles). I can't figure out how to make it with without extra() method. Any ideas? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---