Search code examples
djangodjango-modelsuser-management

Adding a new user to Django, by directly writing in the DB


I need to programmatically add a new user to a Django setup from a 3rd party platform (PHP) and it seems that I only need to write a single entry in the auth_user table to achieve this.

Am I correct? Do I need to do something else also?

Thanks!


Solution

  • This will work, assuming you don't mind re-implementing the things that Django does for you; particularly hashing the password, but possibly also creating a UserProfile object as Ignacio suggests. You'd also need to work out another solution for code relying on post_save or pre_save signals in your project (if there is any).

    You haven't explained why you want to do things this way: what is dictating that you can't have any inter-process communication apart from shared access to an SQL database?

    If you can do things differently, it might make more sense to set up a localhost-only API endpoint to create users, either using the included UserCreationForm or the create_user() helper method.