[Answer]-Creating OneToOneField with base model

1👍

I think, You should not split your models with onetooneField because of following reasons

  • As you said there will be some extra code to manage them.
  • Every time you query them you will have to make two queries instead of two.

Please don’t forget that django models has two functions. The keep data related methods and they keep data model of your application. Some bussiness models have tables that have hundreds of fields. This is completely normal. If you really want to split them. you might want to check out abstract base classes. those are base classes for your model that does not have a seperate tables for themselves https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes

But if you insist on going with oneToOne field you can wrap object creation code in one of the model’s method like

MyMode.create(attr_for_model_A=1, attr_for_model_B=2)

Or you can overwrite default manager’s create method to create two method instead of one
https://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets

In my opinion, non-of those will worth having small model code.

Leave a comment