Hi,
I've been trying to use PublishedManager's `published` method to build custom queries for some custom logic. What's bothering me is that `published` only works with PublishedManager, so I can only do SomeDisaplayable.objects.published(user).blahblahblah. This makes it more difficult to reuse QuerySet objects, especially if I want to apply `published` to a ForeignKey reverse (for example, filtering user.blogposts).
My current solution is to use __in like this
published = BlogPosts.published(user)
viewable_posts = some_author.blogposts.filter(id__in=published.values_list('id', flat=True))
which is obviously not very scalable. What I can think of is to extract the Q object from `published` into another method (property?) like this
class PublishedManager(Manager):
def published(self, for_user=None):
return self.filter(self.get_published_query(for_user))
def get_published_query(self, for_user=None):
if for_user is not None and for_user.is_staff:
return Q(pk__isnull=False) # Another way of saying all()
return (
Q(publish_date__lte=now()) | Q(publish_date__isnull=True),
Q(expiry_date__gte=now()) | Q(expiry_date__isnull=True),
Q(status=CONTENT_STATUS_PUBLISHED))
So that in the above scenario we can have
viewable_posts = some_author.blogposts.filter(BlogPosts.objects.get_published_query(user))
which don't have a scalability problem.
What I wish to know is, am I missing a better way of doing this? I was planning to send a pull request, but figure that should ask about it first.
p.s. `get_published_query` is probably not a good name, but I am out of ideas right now. Any suggestion is welcome. The pk__isnull thing seems dumb too.