django - subclassing a model just to add new methods
<p>I need to subclass a model from a third-party app (<code>django-oscar</code>).</p>

<p>If i do this</p>

<pre><code>from oscar.apps.catalogue.models import Category

class NewCategory(Category):
    @property
    def product_count(self):
        return self.product_set.all().count()

    class Meta:
        db_table = 'catalogue_category'
</code></pre>

<p>Django will think that it is a multi-table inheritance, and <code>NewCategory</code> is a child model for <code>Category</code>. This will result in errors such as  </p>

<pre><code>OperationalError at /api/categories/
no such column: catalogue_category.category_ptr_id
</code></pre>

<p>I can get away with this</p>

<pre><code>def product_count(self):
    return self.product_set.all().count()

Category.product_count = product_count
</code></pre>

<p>but this doesn't seem nice, plus I am unable to add a <code>@property</code> decorator this way.</p>

<p>Is there a cleaner way to do this?</p>

<p>You need a <a href="https://docs.djangoproject.com/en/1.10/topics/db/models/#proxy-models" rel="nofollow">proxy model</a>.</p>

<pre><code>class NewCategory(Category):
    class Meta:
        proxy = True

    ...
</code></pre>

python django