[Answer]-Django Custom Users Adding Second User Model

1👍

You can only have one ‘official’ user model in your project:
https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#substituting-a-custom-user-model

I suggest you organise it like this:

class StandardUser(AbstractEmailUser):
    class Meta:
        app_label = 'accounts'

class CompanyUser(StandardUser):
    company = models.CharField(max_length=100)

    class Meta:
        app_label = 'accounts'

and in settings.py

AUTH_USER_MODEL = 'myapp.StandardUser'

In other words every CompanyUser has an associated StandardUser via the auto OneToOneField as per Django model inheritance.

This approach is akin to Object composition and I think it’s probably the only approach that is going to work in Django.

This means in order to query non-company users you have to do something like:

StandardUser.objects.filter(companyuser=None)

(you may want a custom queryset manager for this

Probably if you go this route the AbstractEmailUser class is no longer needed, you could rename it and make that your concrete StandardUser class.

Leave a comment