I was told that doesn't make sense as managers operate on all rows not a single instance but I see what I want to achieve done in django-taggit library.
Here: https://github.com/alex/django-taggit/blob/master/taggit/managers.py
And the the installation works as follows:
from django.db import models
from taggit.managers import TaggableManager
class Food(models.Model):
# ... fields here
tags = TaggableManager()
Then to tag anything, one can simply do the following:
apple.tags.add("red", "green", "fruit")
Note: apple
not Apple
.
Yet, when I try to do it myself, I get: AttributeError: Manager isn't accessible via MyModelName instances
!
My code:
from django.db import models
class TagManager(models.Manager):
def add(self, *tags):
print("Testing...")
class List(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=500)
tags = TagManager()
Then I am trying to call the add method as follows:
l = List.objects.create(...)
l.tags.add(...)
How can I make it work?