Hi, On 28 Okt., 10:56, Daniel Roseman <[EMAIL PROTECTED]> wrote: > 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
Many thanks, but the method above is a method of Author objects, so self refers to the current author. I need to get all elements (pages) written by that author if (and only if) the parent page has a different author. The literal SQL would be something like: SELECT pages.id, pages.author_id, motherpages.author_id AS motherauthor FROM pages, pages AS motherpages WHERE motherpages.page_id = pages.mother_id AND pages.author_id = 1234 AND pages.author_id <> motherpages.author_id > 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) Sorry, that was misleading – I need all pages heading a sub-tree written by the same author. These pages are not necessarily top-level pages. > > (BTW in English we would usually say 'parent' rather than 'mother'...) Thanks – the name "mother" was chosen for historical reasons. Cheers, Jan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---