[Django]-Django – Extending an app's model to override its manager

1👍

Using Secator’s suggestion as part of the solution, I ended up just monkey-patching the Tag model itself where needed instead of the Tag.objects.

This being the final code:

from tagging.models import TagManager, Tag
import tagging

class MyTagManager(TagManager):
    def update_tags(self, obj, tag_names):
        # My actions
        return super(MyTagManager, self).update_tags(obj, tag_names)
    def add_tag(self, obj, tag_name):
        # My actions
        return super(MyTagManager, self).add_tag(obj, tag_name)

class MyTag(Tag):
    objects = MyTagManager()
    class Meta:
        proxy = True

tagging.models.Tag = MyTag
tagging.fields.Tag = MyTag
👤Cruel

2👍

Use proxy model with the different manager:

class MyTag(Tag):
    objects = MyTagManager()
    class Meta:
        proxy = True

Leave a comment