django autoupdate user; save() got an unexpected keyword argument 'force_insert'
我正在尝试为使用modeladmin save_模型创建记录的用户实现自动更新,如本文所述。我无法解决如何绕过Kwarg错误"save()得到了一个意外的关键字参数‘force_insert’"。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | admin.py from myapp.myproj.models import Activity from django.contrib import admin class ActivityAdmin(admin.ModelAdmin): exclude = ('cruser',) list_display = ('activity_nm', 'activity_desc', 'startdt', 'enddt','upddt','crdt') def save_model(self, request, obj, form, change): if not change: obj.cruser = request.user obj.save() admin.site.register(Activity, ActivityAdmin) |
文件规定:
"The save_model method is given the HttpRequest, a model instance, a
ModelForm instance and a boolean value based on whether it is adding
or changing the object."
这是自动的还是我需要从视图中传递它?如果这不是问题所在,那还能是什么呢?
编辑:修改代码以匹配示例。
更新
如果您重写了
1 2 3 4 5 6 7 8 | def save(self): ... # should be def save(self, force_insert=False, force_update=False, using=None): ... # or at least def save(self, **kwargs): ... |
检查跟踪以找到故障的
您的代码没有完全遵循b-list.org中的代码,请尝试:
1 2 3 4 | def save_model(self, request, obj, form, change): if not change: obj.cruser = request.user obj.save() |
作为一般规则,您应该只在模型本身中重写
在模型中重写save()方法时,应始终使用
您的
1 2 3 | def save(self, *args, **kwargs): '''do your custom stuff here''' return super(Activity, self).save(*args, **kwargs) |