4

I have some models using django-polymorphic-model

eg.

class Article(PolymorphicModel):
  ...
class Blog(Article):
  tags = ...
class Story(Article):
  publish = ...

Normally if I get all articles, I just do Article.objects.all(), however what if I want to get all articles that tags are empty? If I do Articles.objects.filter(tags__isnull=True) it will break because other models don't have this field, I would like to include Story entries too, do I really have to split into 2 different queries and combine again?

James Lin
  • 25,028
  • 36
  • 133
  • 233

2 Answers2

5

OK After some digging for the documentation through issues, here is how to do it

Articles.objects.filter(Blog___tags__isnull=True)
James Lin
  • 25,028
  • 36
  • 133
  • 233
  • 1
    What if you have multiple subclasses with the same field, is there a way to filter across all subclasses with tags__isnull=True? – owenfi Apr 10 '15 at 11:45
  • I don't have the code with me now, but I think if all subclasses have the field, it should work, even better, if all subclasses have same field, that field should sit on the parent class, so it definitely works. – James Lin Jun 21 '18 at 20:05
2

Hope you found an answer before.

Articles.objects.filter(Q(Blog___tags__isnull=True)|Q(Story__tags__isnull=True))
user2652620
  • 464
  • 2
  • 17