Convert OneToOneField to MultipleTableInheritance

孤人 提交于 2019-12-12 03:59:23

问题


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:


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.



来源:https://stackoverflow.com/questions/32761441/convert-onetoonefield-to-multipletableinheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!