Is there a simpler way to add a user than with the following pattern?
try: new_user = User.objects.create_user(username, email, password) except IntegrityError: messages.info(request, "This user already exists.") else: new_user.first_name = first_name # continue with other things
In Django 1.4, get_or_create() exists for User.
from django.contrib.auth.models import User _user = User.objects.get_or_create( username=u'bob', password=u'bobspassword', )
It is better not to catch IntegrityError
as it can happen for other reasons. You need to check if user exists, excluding the password. If user already exists, set the password.
user, created = User.objects.get_or_create(username=username, email=email) if created: user.set_password(password) user.save()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With