I'm using Django's postgres-specific ArrayAgg
aggregator. It works fine but when the list is empty I get [None]
instead of []
. Is there any way to filter these null values out? I've tried to pass a filter argument to ArrayAgg
but it didn't work. Here's a simplified example of my setup:
class Image(models.Model):
# ...
class Reporter(models.Model):
# ...
class Article(models.Model):
reporter = models.ForeignKey(Reporter, related_name='articles')
featured_image = models.ForeignKey(Image, related_name='articles')
# ...
Then if I make this query:
reporter = Reporter.objects.annotate(
article_images=ArrayAgg('articles__featured_image'),
distinct=True
).first()
And the first reporter in the result set doesn't have any associated article, I get:
> reporter.article_images
[None]
I've tried to add a filter, but no luck:
Reporter.objects.annotate(
article_images=ArrayAgg(
'articles__featured_image',
filter=Q(articles__featured_image__isnull=False)
)
)