Convert OneToOneField to MultipleTableInheritance

2019-09-07 03:04发布

Build of this question: Which is better: Foreign Keys or Model Inheritance?

I would like to know if it is possible to replace a OneToOne field by MTI?

A.k.

I have:

class GroupUser(models.Model):
    group = models.OneToOneField(Group)
    ...other fields....

and I want:

class GroupUser(Group):
    ...other fields....

I think that should be faster, or not?

Is it possible?

1条回答
来,给爷笑一个
2楼-- · 2019-09-07 03:42

It won't be faster, because your parent class object will still have a field in the database that links to the child class if you are using concrete inheritance(and sounds like it would be), so technically the efficiency is the same as OneToOne field.

The choice is also based on the business logic. Inheritance is used for the situations where you have things that are of similar type, so that you could define common fields/methods in the parent class and reduce some repetitive code. From your example sounds like Group and GroupUser are totally two different things, most likely they don't share many common attributes either, so unless I misunderstand your intention, OneToOneField is a better candidate.

查看更多
登录 后发表回答