How to use a custom manager method from model relationship
Lets say I have 2 model class. Category has a name and multiple tags and
Tag has a name, can be visible or not. I would like to do something like
this :
visible_tags_for_my_category = c.tags.get_visible_tags()
where "c" represents a category instance with some tags and
get_visible_tags() would return all the tags where visible=True that the
category has.
Should I use a custom manager to do this ? If yes, how can I use the
custom manager from tag with category?
class Category(models.Model):
name = models.CharField(max_length=255, unique=True)
tags = models.ManyToManyField(Tag)
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
visible = models.BooleanField(default=False)
No comments:
Post a Comment