jl6
jl6

Reputation: 6394

Django: How do I create a new model containing all the fields of another model (but unrelated to it)?

Given an existing Django model:

class MyModel(models.Model):
    field1 = ...
    field2 = ...
    ...
    fieldN = ...

I would like to create a wholly separate model that looks like this:

class MyModel2(models.Model):
    field1 = ...
    field2 = ...
    ...
    fieldN = ...

    fieldA = ...
    fieldB = ...
    ...
    fieldZ = ...

Multi-table inheritance does not work for me as I want MyModel2 to be backed by a database table containing all of its fields, not just the extra ones plus a link to the MyModel table.

Defining an Abstract Base Class might work if I could change the definition of MyModel. But is there another way, possibly using the Python type metaclass?

Upvotes: 1

Views: 154

Answers (1)

Marcin
Marcin

Reputation: 49816

Why not an abstract base class for both, and one of your concrete classes has no further fields?

Upvotes: 1

Related Questions