1

For a SaaS Application, I would like to have a custom subsite for each Client. Each subsite will have Users, for which I would like to use Django's default User Model.

The subsite is defined by the subdomain, so e.g. client1.example.com and client2.example.com will be two different subsites that run on the same application.

I would like to have unique Users in each subsite, so e.g. alice@email.com would be able to sign up for multiple subsites with the same username (email).

How can I alter the Django User Model to also include a client_id and then perform a unique_together constraint on both client_id and username? Do I have to rewrite the whole authentication backend for this?

Glenn
  • 67
  • 2
  • 6
  • Did you find a solution for this? Currently trying to do it myself and using Tenant schema, but it won't let me create a shared user. – JDavies Jun 13 '19 at 19:52

1 Answers1

0

I think it'll be sufficient to define your own user model.

Derive from AbstractBaseUser

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#specifying-a-custom-user-model

and you will need unique_together = ('client_id', 'username',) https://docs.djangoproject.com/en/1.7/ref/models/options/#unique-together

edit:

found solution in SO

Django 1.7 multisite User model

He basically create a field which combines the two field for USERNAME_FIELD. I'm not sure if there's a more elegant way of doing it.

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Unfortunately, the sites framework does not work with subdomains, as each site needs a unique SITE_ID in settings.py, so this is not an option for me I'm afraid. – Glenn Feb 26 '15 at 16:05
  • umm? can't you just use client_id in place of his site_id? – eugene Feb 27 '15 at 01:55