6

I'm subclassing an existing model (from another application entirely) and want my model to have its own database table. An identical clone/replica of the original table, not just a table with a pointer to the data stored in the "parent" table.

Here's my model:

class A (models.Model):
    name = models.CharField('name')

class MyA (A):
    class Meta:
        db_table = 'My_A'

Here's my DB tables:

CREATE TABLE A
(
  id serial NOT NULL,
  "name" character varying(50) NOT NULL,
  ...
)

CREATE TABLE My_A
(
  A_ptr_id integer NOT NULL,
  ...
)

And here's what I would like to have:

CREATE TABLE A
(
  id serial NOT NULL,
  "name" character varying(50) NOT NULL,
  ...
)

CREATE TABLE My_A
(
  id serial NOT NULL,
  "name" character varying(50) NOT NULL,
  ...
)

Edit: I ended up copy-pasting the 3rd party model

2
  • What is the difference between MyA and A? Commented Aug 22, 2012 at 7:37
  • 2
    None really. Model A is imported from another application (also used for it's primary purpose in my project) and I want to separate the stored data. Commented Aug 22, 2012 at 8:54

3 Answers 3

4

If I am understanding you correctly, you can create an abstract base class that won't have its own table. From there you could then create multiple classes that inherit from that base class that have their own tables.

class ABase(models.Model):
    name = models.CharField('name')
    class Meta:
        abstract = True

class A(ABase):
    class Meta:
        db_table = 'A'

class MyA(ABase):
    class Meta:
        db_table = 'My_A'

Django 2.2 Documentation - Abstract base classes

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

Comments

0

New approach (if you insist on separating the data): create model MyA without inheriting from A and create another model MergeA that creates explicit generic foreign key to My and A. But this will certainly require quite some extra coding.

Comments

-1

When you inherit a Django Model it will always generate an implict 1-1 relation to the parent model. And Django will only generate the fields for your inherited model. Which in general makes total sense to me. But for your use case is cumbersome. Since we are taling about Django core functionality, it might be hard to workaround. You could try to copy and paste your 3rd party model into your code as a proxy model and inherit from there.

Update: But why do you want to separate the data into different tables? What is the final objective?

1 Comment

The 3rd party model(A) is also used for it's primary purpose in my project. And I need to keep them separate so that A.objects.all() only returns the "original" objects. I also would need to run offsite computations to regularly re-populate MyA-table via fixtures (or something) and I don't want to affect the production use of model/table A.

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.