29đź‘Ť
Warning: Django 1.5 is very new and the people are still investigating its new features. So my answer is nothing more than my opinion, based on recent research to answer this question.
Both ways are valid ways to achieve the result, with its advantages and disadvantages.
Let’s start with the:
Second option
- Without nested models and not modular.
AbstractBaseUser
, as the name says, is an abstract model and does not have a specific table - Has unused fields
-
You need to check the user_type for any iteration with the model that uses the extra fields:
def foo(): if user.user_type == 'Private': # ... else: # ...
The resulting SQL would be approximately as follows:
CREATE TABLE "myapp_user" (
"id" integer NOT NULL PRIMARY KEY,
"password" varchar(128) NOT NULL,
"last_login" datetime NOT NULL,
"email" varchar(254) NOT NULL UNIQUE,
"user_type" varchar(30) NOT NULL,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL,
"company_name" varchar(100) NOT NULL
);
First option
- Nested models with logical separation of entities
- Very lean
- You must implement
BaseUserManager
for each child if you want to usecreate_user
-like functions - You cannot access the subclasses with a simple
BaseUser.objects.all()
*
The resulting SQL would be approximately as follows:
CREATE TABLE "myapp_baseuser" (
"id" integer NOT NULL PRIMARY KEY,
"password" varchar(128) NOT NULL,
"last_login" datetime NOT NULL,
"email" varchar(254) NOT NULL UNIQUE
);
CREATE TABLE "myapp_privateuser" (
"baseuser_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_baseuser" ("id"),
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
CREATE TABLE "myapp_tradeuser" (
"baseuser_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_baseuser" ("id"),
"company_name" varchar(100) NOT NULL
);
* Imagine the following situation:
>>> BaseUser.objects.create_user('baseuser@users.com', password='baseuser')
>>> PrivateUser.objects.create_user('privateuser@users.com', password='privateuser', first_name='His', last_name='Name')
>>> TradeUser.objects.create_user('tradeuser@users.com', password='tradeuser', company_name='Tech Inc.')
>>> BaseUser.objects.all()
[<BaseUser: baseuser@users.com>, <BaseUser: privateuser@users.com>, <BaseUser: tradeuser@users.com>]
>>> PrivateUser.objects.all()
[<PrivateUser: privateuser@users.com>]
>>> TradeUser.objects.all()
[<TradeUser: tradeuser@users.com>]
So, you cannot directly retrieve the subclasses instances by using BaseUser.objects.all()
. There is an excellent blog post by Jeff explaining better how to accomplish “automatic downcast” from BaseUser
to its childs.
That said, you should consider the advantages and disadvantages of each approach and their impact on your project. When the involved logic is small (as in the example described), both approaches are valid. But in a more complex scenario, an approach can be better than the other one. I would choose the multi model option because it is more extensible.
- [Django]-Django error message "Add a related_name argument to the definition"
- [Django]-How to use dynamic foreignkey in Django?
- [Django]-What is the equivalent of django.db.models.loading.get_model() in Django 1.9?
2đź‘Ť
The new custom user model you can assign only one model to AUTH_USER_MODEL. With multi-table inheritance you have two models. So that is a problem.
In the case of a single user model that covers both user types you could abstract the conditional logic in the model methods. You can also use different managers for different user types based on how much they are different. This could also help you in being explicit when working with a specific user type.
Other option could be to store only most common attributes in a single user model and then attach specifics of two user types into their own tables which are linked to your main user table.
If both users have most of the things in common (in terms of data at least), I would keep them in one place. In the end, I would think about what is simpler and easier to maintain.
- [Django]-How do I get the current date and current time only respectively in Django?
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Django REST framework post array of objects
1đź‘Ť
I’d use a single model with a “type” attribute. Here is why:
- Only one table, only one model
- If you want to convert from one type to another you just change attribute
- Implement getters and setters for the fields that exists for one type and doesn’t exists for other = much more simple to use.
- [Django]-How can I render a ManyToManyField as checkboxes?
- [Django]-Python: Getting the error message of an exception
- [Django]-How to use python2.7 pip instead of default pip