我需要創建用戶使他的名字獨特的邏輯小的幫助:Django的 - 創造用戶使他們的全名唯一
我有一個Django的用戶配置文件。我以這種方式創建用戶:
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()
非常好,這確保來自世界2個地區的兩個人不會設置相同的名稱,對吧? – doniyor
這是正確的。並且它在數據庫級別執行,所以您不會遇到兩個線程同時設置相同名稱的難得機會。 – sdolan
''userprofile.fullname = fullname''應該是''userprofile.fullname = new_fullname'',對吧? – doniyor