Search code examples
djangomulti-tenantdjango-users

Django custom user model, with unique togethr - tenant and username


I am making a multi tenant app on Django, where I want the user model to be unique together on "tenant" (foreign key to tenant model,having the tenant details) and "username", ie something like - unique_together =("username", "tenant") or any of its variety. Is there any possibility of doing that?


Solution

  • Django user model need "username" to be unique, therefore I will suggest to use another field to replace the username such as "email" which i believe you want to keep unique

    In order to do that you need to override the user model and change the username field

    USERNAME_FIELD = 'email'