I have model for pages that consist of a number of content blocks.
class Page(models.Model):
...
class Block(models.Model):
page = models.ForeignKey(Page)
...
The Block
has a handful of other properties that determine whether it is considered to be "active" or not (a couple booleans and a datetime field). I have a manager for the Block
model so that I can get a list of active Blocks
Block.objects.active()
Page.objects.first().block_set.active()
I want to be able to write a queryset to return only Page
objects that have active blocks. I would like to do this using the existing Block
active manager, so that I am only defining what makes an "active" block once (DRY). IE something like:
Page.objects.annotate(count=Count('block__active')).filter(count__gt=0)
Obviously that does not work since active
is not a property of Block
. Is there a way I can use the existing Block
manager to achieve this?