在django的admin自定义以 quote_no 为唯一键的时候,另存为出现报错,需要手动修改,写了个函数,但是不知道怎么拦截或者修改。
找了好多资料,参考这个# [django]更改 request.GET 字典的键值时出现 "AttributeError: This QueryDict instance is immutable" 错误
在ModelAdmin中,加入
class FactoryQuoteAdmin(admin.ModelAdmin):
save_as = True
# 当每次修改数据的时候会调用这个函数
def change_view(self, request, object_id, form_url='', extra_context=None):
# 检查是否点击 另存为新的
if "_saveasnew" in request.POST:
request.POST = request.POST.copy()
# 改为自己写的生成随机生成函数
# 因为默认的 QueryDict 是不可修改的。解决办法就是复制一份副本,对副本进行修改:
request.POST["quote_no"] = get_facotry_quotation_code()
return super(FactoryQuoteAdmin, self).change_view(request, object_id, form_url, extra_context)
网友评论