On Oct 28, 9:33 am, janedenone <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I need to exclude certain elements from a QuerySet. My models look
> like this (simplified):
>
> class Author(models.Model):
> first_name = models.CharField(max_length=90, blank=True)
> last_name = models.CharField(max_length=90, blank=True)
>
> class Page(models.Model):
> author = models.ForeignKey(Author)
> mother = models.ForeignKey('self', blank=True, null=True,
> related_name='child_set')
>
> For each author, I need to get the set of pages representing all top-
> level works by that author (so I don't need the chapters, sections
> etc.). I guess the solution looks something:
>
> def _get_top_level_children(self):
> return self.page_set.exclude(author=mother.author)
> top_level_children = property(_get_top_level_children)
>
> Obviously, it does not work like this. How can I limit the queryset to
> all entries where the 'author' object of a page does not match the
> 'author' object of the page's 'mother' object?
>
> Kind regards,
> Jan
Well the only thing wrong with your code is you need a self in there -
author=self.mother.author
But I wouldn't do it like that. If you just want all top-level Page
objects, you could do
Page.objects.filter(mother__isnull=True)
and if you want all top-level Pages belonging to a particular author,
it would be
author.page_set.filter(mother__isnull=True)
(BTW in English we would usually say 'parent' rather than 'mother'...)
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---