Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - Extending an app's model to override its manager
    text
    copied!<p>Using the <code>django-tagging</code> app as an example, I would like to change the manager of the <code>Tag</code> model so that I can replace it with an extended one:</p> <pre><code># mytagging/models.py from django.db import models from tagging.models import TagManager, Tag 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) Tag.objects = MyTagManager </code></pre> <p>Now, <code>Tag.objects = MyTagManager</code> doesn't work, nor did I expect it to, but it illustrates what I would like to accomplish. I could very well create <code>class MyTag(Tag)</code> and set the manager that way, but then it would seem that I would also have to extend every other class that uses <code>Tag</code> and change it to <code>MyTag</code>.</p> <p>I tried <code>Tag.objects = MyTagManager()</code> to initialize the class, but I get the error <code>'NoneType' object has no attribute '_meta'</code> from a query object, it appears.</p> <p>The above code renders this error when calling <code>Tag.objects.update_tags(kwargs['instance'], tags)</code> from admin page:</p> <blockquote> <p>unbound method update_tags() must be called with MyTagManager instance as first argument (got LibraryFile instance instead)</p> </blockquote> <p>The <code>LibraryFile</code> model is the one I'm attempting to tag, and should therefore be the second argument instead of first (<code>self</code> being first).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload