简体   繁体   English

关键字未传递功能

[英]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.

相关问题 将字典作为关键字参数传递给函数 - Passing a dictionary to as keyword parameters to a function 将参数作为关键字传递给新的 function - Passing parameter as keyword to new function 将字典作为关键字参数传递给函数 - Passing a dictionary to a function as keyword parameters 解开字典并将其作为关键字参数传递给函数 - Unpacking a dictionary and passing to a function as keyword parameters 将可选的关键字参数传递给装饰函数 - Passing optional keyword arguments to a decorated function 当外部函数具有同名的关键字参数时,将关键字参数传递给内部函数 - passing keyword argument to inner function when outer function has keyword argument with the same name 将关键字参数传递给使用位置参数定义的函数时,会产生误导(?)TypeError - Misleading(?) TypeError when passing keyword arguments to function defined with positional arguments 将字典传递给以元组为键的 function 时生成关键字错误 - keyword error generated when Passing a dictionary to a function with tuples as the keys 使用连字符传递关键字参数 - Passing keyword arguments with a hyphen 当局部变量名称与 function 参数名称相同时,将关键字 arguments 传递给 function - Passing keyword arguments to a function when local variable names are same as function parameter names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM