pythondjangomulti-table-inheritance

Inherit from non-abstract model without multi-table inheritance


Is there a way to inherit from an existing model in order to copy of all its fields (in a DRY manner) without invoking multitable inheritance if the model isn't abstract?

To be clear, I have a model Post, and I'd like to have another model GhostPost that mirrors Post exactly in terms of the available fields, but I do not want it to have multitable inheritance or any kind of relationship with Post. The problem is, Post isn't an abstract model, so Django will initiate multitable inheritance. Is there a way around this?

Update: What I'm looking for here is not a Python-level duplicate of the model, but an actual separate database table that mirrors those fields.

Solution: I followed @Brandon's advice and used an abstract model.


Solution

  • You need to use a Proxy model in this case. It will allow you to extend a model that is not abstract but without mutli-table inheritance: https://docs.djangoproject.com/en/1.6/topics/db/models/#proxy-models

    A proxy model will keep a single-table, so if you need one table for each model, I would suggest making a common abstract class to inherit from for the Post and GhostPost models.