[英]Keyword Not Passing in Function
I am working with django models. 我正在使用Django模型。 I want to pass a model field as a variable.
我想将模型字段作为变量传递。 Given my function:
鉴于我的职能:
from django.models import models
def updatetable(value, fieldtitle, tablename, uid, refname):
workingobj = tablename.objects.get(refname=uid)
currentvalue = getattr(workingobj, fieldtitle)
setattr(workingobj, fieldtitle, currentvalue + value)
workingobj.save()
return
I have tried: 我努力了:
updatetable(len(sr), 'posts_added', managementmetrics, startdtg, refname=update_dtg_start)
updatetable(len(sr), 'posts_added', managementmetrics, startdtg, refname='update_dtg_start')
and even 乃至
updatetable(len(sr), 'posts_added', managementmetrics, startdtg, {refname:update_dtg_start})
I get the error: Cannot resolve keyword 'refname' into field. 我收到错误消息:无法将关键字“ refname”解析为字段。 Choices are: length_of_update, update_dtg_finish, update_dtg_start
选项包括:length_of_update,update_dtg_finish,update_dtg_start
I've tried switching out refname for **kwargs but still can't seem to get it to take the field value. 我曾尝试将refname替换为** kwargs,但似乎仍然无法获取该字段的值。
The problem is not in how you call this function: the function itself does not do what you want. 问题不在于您如何调用此函数:该函数本身无法执行您想要的操作。
You need to change how you call get
. 您需要更改
get
。 Rather than passing in refname directly, you need to use the dict there : 而不是refname直接传递,你需要使用字典有 :
workingobj = tablename.objects.get(**{refname: uid})
Now you simply call the function in the normal way: 现在,您只需以常规方式调用该函数:
updatetable(len(sr), 'posts_added', managementmetrics, startdtg, 'update_dtg_start')
(You should also consider renaming the tablename
parameter: you are not passing a table name, which implies a string, but a model class object.) (您还应该考虑重命名
tablename
参数:您不是在传递表名,这意味着一个字符串,而是一个模型类对象。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.