Tad*_*eck 5 django django-models django-authentication django-users
我一直在寻找最佳实践,但我没有找到它.我甚至无法找到其他人需要使用的解决方案.
我需要根据他的其他数据(名字和姓氏)生成用户的用户名,最后可选择附加整数,直到我获得唯一的用户名.
我更喜欢在模型中这样做.有没有一些标准的方法呢?或者它只适用于表格?我一直在研究各种User模型方法的重载,以及信号,并没有找到任何适当的地方我可以添加它.
一种可能的解决方案是通过pre_save信号。
def my_callback(sender, **kwargs):
obj = kwargs['instance']
if not obj.id:
username = get_unique_username(obj) # method that combines first name and last name then query on User model, if record found, will append integer 1 and then query again, until found unique username
obj.username = username
pre_save.connect(my_callback, sender=User)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2184 次 |
| 最近记录: |