我正在尝试在Django中创建UserProfile模型。该模型中字段的信息的一半将直接来自用户以表格形式输入的内容,而另一半则是我从他们登录时进行身份验证的社交网络中导入的。我将如何实施呢?
if form.is_valid():
prefs = form.save(commit=False)
prefs.user = request.user
prefs.save()
# if they logged in through social network there are other fields
if socialacc:
firstname = socialacc[0].extra_data['firstName']
因此,现在我将“ firstname”的数据作为字符串。我应该如何将其保存到相同的模型?该模型将包含一个名为firstname的字段,除非有可用的社交身份验证信息,否则该字段将为空。
非常感谢您的帮助!
由于该save
方法返回Model object
,因此您可以在将其最终保存到数据库之前再次对其进行修改:
if form.is_valid():
prefs = form.save(commit=False)
prefs.user = request.user
# if they logged in through social network there are other fields
if socialacc:
prefs.firstname = socialacc[0].extra_data['firstName']
...
prefs.save()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句