3

I need to define a extact copy of existing model with it's own table and all columns without Django inheritance mechanism. Otherwise it uses OneToOne relation and keeps all duplicated fields in parent table, that I definetly don't need. I just want to avoid repeating model fields and method definitions for my second model.

Any suggestions?

2 Answers 2

6

One way would be to create abstract base model with common attributes. Then create one model corresponding to parent model in current app.

Create another model for the duplicate model with same base class (and some other fields).

Not elegant though!

Sign up to request clarification or add additional context in comments.

Comments

0

Did you look at Mixins?

With them you can mixin the fields of a class to your Model class and still inherit form a regular Base class. And you can mixin fileds from different classes and thus maybe make a good structure.

http://eflorenzano.com/blog/2008/05/17/exploring-mixins-django-model-inheritance/

5 Comments

The problem is the same - inheritance from mixins classes or probably any non-abstract classes makes OneToOne relation and keeps all inherited fields in parent table, that I definetly don't need.
Make a Mixin containing all the fields and make it abstract. Make a Base Class. Make the two concrete classes and inherit form the field-less base class and add the mixins each.
what difference from the first answer with inheritance from abstract class? could you show example of structure in your answer?
Technically, there is no difference. But it allows you to have different base classes for example. Why don't you accept the first answer?
I'm still thinking about the way I prefer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.