class AccountAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.security = get_random_code(10)
obj.password = get_password(obj.password, obj.security)
obj.save()
Token.objects.create(user=obj)
admin.site.register(Account, AccountAdmin)
save_model()方法里这么多参数用来干什么,还是默认的?
看了下调用,根本就没有传入这么多值嘛。
怪我咯2017-04-17 17:47:32
I just searched the source code, where save_model is called, all with these parameters,
For example, Example 1 and Example 2
怪我咯2017-04-17 17:47:32
The answer is: You can easily perform some operations before or after saving. For example, in the code you posted, a new Token is created after saving 前
设置了security
保存后
. The business logic is easy to implement.
obj is the saved object, form is a ModelForm instance, change is a bool value, indicating whether the obj has been changed. If there is no change, the save method may not be called.