HatTricks
HatTricks

Reputation: 221

What's the best way to deal with superusers being created differently?

In my Django 1.6.5 project, my user registration form creates a UserProfile that has a one-to-one relationship with the created User. My views assume that when a user is logged in, s/he has an associated UserProfile in the database.

However, superusers are created through the manage.py file, and don't have an associated UserProfile, which causes the views to throw an error. This isn't a huge issue, since I will be the only one with a superuser login, but it still seems like a bad idea to leave the website like that.

There's nothing the app can really do if there is no UserProfile, so should I code in a special case where the views throw an explicit error warning the user that s/he is logged in as a superuser and cannot use the website? I could also make a UserProfile manually for the superuser, but that seems silly.

Or is there a way to enforce the existence of a UserProfile for each User at the database level? If so, how would I create a superuser simultaneously with a UserProfile, since it's being created from the command line?

Upvotes: 2

Views: 56

Answers (1)

John Mee
John Mee

Reputation: 52313

Keep them separate:

  • If you want to see what it looks like as a user use a user account like everyone else.
  • If you want to mess with the admin use your superuser account for that.
  • If the application is big enough I have been known to add a 'loginas' function to the admin so I can become the user (for support purposes so I don't have to ask their password).
  • for testing environments I've been known to run a script that changes the password of all users to the same thing (so I can test lots of accounts/scenarios freely)

Although it's probably more conventional to Inherit/Extend/Encapsulate your User with/from the default django User. Here we go... docs for extending the user model

Upvotes: 1

Related Questions