0

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?

Adam
  • 2,948
  • 10
  • 43
  • 74
  • 1
    How did you construct this? Please post the *full* traceback together with the code that you run. – Willem Van Onsem Sep 23 '18 at 17:10
  • @WillemVanOnsem Done. Thanks. – Adam Sep 23 '18 at 17:16
  • 2
    There's a lot more going on here. Despite the name, [TaggableManager is actually a *field*](https://github.com/alex/django-taggit/blob/master/taggit/managers.py#L361), that emulates some of a many-to-many field. You can't do this with a manager. – Daniel Roseman Sep 23 '18 at 17:21
  • @DanielRoseman: Okay, that makes total sense. I understand the trick now! Thank you. – Adam Sep 23 '18 at 17:27

0 Answers0