I have a model called Item:
class Item(models.Model):
...
Also I have another model called Content. It has relation to Item and a ChoiceField to select is the content is active or not:
class Content(models.Model):
item = ForeignKey(Item related_name=)
is_active = BooleanField(default=False)
content = TextField()
def save(self, *args, **kwargs):
"""There's a logic to make sure every item has only one content"""
I have two questions.
- How to filter out the Items without any content or without any active content.
- Can I do something like alias to call item.content to return active content of the item without causing db performance issues.
You filter with:
Given the related name looks like:
You can prefetch these
Contents to thecontentswith:For these
Items, it will set the.contents.all()to the relatedContents withcontentnot being empty, andis_activebeingTrue.