You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defsave(self, commit=True):
# Save the provided password in hashed formatuser=super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
# if commit:# user.source = 'adminsite'# user.save()returnuser
修改前后对比:
当前大体环境配置: Django==4.2.14 Windows10 MySQL==8.0
The text was updated successfully, but these errors were encountered:
JOYCAT-Q
changed the title
管理员创建普通博客账号时重构的save方法中user.source = 'adminsite'不生效
accounts.admin中管理员创建普通博客账号时重构的save方法中user.source = 'adminsite'不生效
Jul 19, 2024
我确定我已经查看了 (标注
[ ]
为[x]
)我要申请 (标注
[ ]
为[x]
)accounts.admin
中的class BlogUserCreationForm(forms.ModelForm)
中输出结果为
"commit: ", False
使得
if
判断后的语句无法正常执行,具体表现为用户的source
设置失效commit
参数为False
的原因:在
Django
管理员中,添加一个新用户时,ModelAdmin
会调用表单的save_model()
方法来保存模型实例。这个方法通常不会直接调用表单的save()
方法,而是接收一个已经保存(但可能未提交到数据库)的模型实例作为参数。解决方法:在
class BlogUserAdmin(UserAdmin)
中重构方法同时将之前判断注释掉:
修改前后对比:
当前大体环境配置:
Django==4.2.14
Windows10
MySQL==8.0
The text was updated successfully, but these errors were encountered: