小编Jow*_*ado的帖子

为什么 Django Admin 使用 commit=False 调用 UserCreationForm save 方法?

django 管理员实际上在哪里保存它的模型?

我想打印一些仅在保存后创建的模型字段,但 UserCreationForm 保存方法总是使用 commit=False 调用,并且似乎返回用户,因此保存发生在其他地方。

class MyUserCreationForm(UserCreationForm):
    ...
    def save(self, commit=True):
        # django admin calling this with commit=False... save occurs somewhere else.
        ...
        if commit:
            print("this never gets printed")
            user.save()

        # line below prints nothing
        print(user.field_set_after_model_is_saved)

        return user
Run Code Online (Sandbox Code Playgroud)

ps:我的模型正常保存,只是没有达到我的预期。

django django-admin

6
推荐指数
1
解决办法
1273
查看次数

标签 统计

django ×1

django-admin ×1