dea*_*ase 0 python django django-models django-forms
我需要在用户注册期间动态生成uniqe用户名.
算法:
当用户的全名是"John Doe"时,用户名应为"jdoe"(如果可用).如果没有,它应该生成jdoe1,jdoe2等,直到用户名可用.
任何现有的简单解决方案?
小智 7
def generate_username(first_name,last_name):
val = "{0}{1}".format(first_name[0],last_name).lower()
x=0
while True:
if x == 0 and User.objects.filter(username=val).count() == 0:
return val
else:
new_val = "{0}{1}".format(val,x)
if User.objects.filter(username=new_val).count() == 0:
return new_val
x += 1
if x > 1000000:
raise Exception("Name is super popular!")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1696 次 |
| 最近记录: |