i need small help in the logic of creating user making his name unique:
i have a django user profile. i am creating users in this way:
fullname = request.POST.get('fullname')
random_username = ''.join(random.sample(string.ascii_lowercase, 8))
new_user = User.objects.create_user(random_username, email, passwort)
##update siteprofile of this new user
userprofile = new_user.get_profile()
"""
i need to make this fullname unique with this logic:
for example john is the fullname of new user. i need to check if there are
other johns in db, if there is another user with this name, i will name the
user with 'john1'. if there are 2, the new user will get the name 'john3'
how can I check this in db in some efficient way?
"""
userprofile.name = fullname
userprofile.save()
You want to check for the IntegrityError
on save and update accordingly. Doing a query to check for the names existence creates a race condition where you can search then two separate threads try to create the same fullname at the same time.
from django.db import transaction
@transaction.commit_manually
def set_fullname(userprofile, fullname, i=0):
new_fullname = u"{}{}".format(fullname, str(i) if i else '')
try:
userprofile.fullname = new_fullname
userprofile.save()
transaction.commit()
return userprofile
except IntegrityError:
transaction.rollback()
i += 1
# Just recursively try until we a valid name. This could be problematic if you
# have a TON of users, but in that case you could just the filter before then to see
# what number to start from.
return set_fullname(userprofile, fullname, i)
userprofile = set_fullname(userprofile, fullname)
For this purpose it would be better to use form https://docs.djangoproject.com/en/dev/topics/forms/ . But if you won't to use forms, you can do it that way:
i = 0
orig_fullname = fullname
created = False
while not created:
profile, created = UserProfile.objects.get_or_create(name=fullname)
if not created:
i+=1
fullname = orig_fullname + str(i)
# there you have new user's profile
Note, that field 'name' in UserProfile model must have unique=True parameter https://docs.djangoproject.com/en/dev/ref/models/fields/#unique
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.