Keyword Not Passing in Function
<p>I am working with django models. I want to pass a model field as a variable. Given my function:</p>

<pre><code>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
</code></pre>

<p>I have tried:</p>

<pre><code>updatetable(len(sr), 'posts_added', managementmetrics, startdtg, refname=update_dtg_start)

updatetable(len(sr), 'posts_added', managementmetrics, startdtg, refname='update_dtg_start')
</code></pre>

<p>and even</p>

<pre><code>updatetable(len(sr), 'posts_added', managementmetrics, startdtg, {refname:update_dtg_start})
</code></pre>

<p>I get the error: Cannot resolve keyword 'refname' into field. Choices are: length_of_update, update_dtg_finish, update_dtg_start</p>

<p>I've tried switching out refname for **kwargs but still can't seem to get it to take the field value. </p>

<p>The problem is not in how you call this function: the function itself does not do what you want.</p>

<p>You need to change how you call <code>get</code>. Rather than passing in refname directly, you need to use the dict <em>there</em>:</p>

<pre><code>workingobj = tablename.objects.get(**{refname: uid})
</code></pre>

<p>Now you simply call the function in the normal way:</p>

<pre><code>updatetable(len(sr), 'posts_added', managementmetrics, startdtg, 'update_dtg_start')
</code></pre>

<p>(You should also consider renaming the <code>tablename</code> parameter: you are not passing a table name, which implies a string, but a model class object.)</p>

python django django-models keyword