2

I have a ajax post that returns the following data:

{u'related_id': u'9', u'db_name': u'my_field', u'name': u'jsaving_draft', u'value': u'hypothesis sadfasdfadfws asdfasdf'}

In my view I have:

if request.is_ajax():                                                                                                                   
     if "jsaving_draft" in request.body:                                                                                                 
         results = json.loads(request.body)                                                                                              
         print results                                                                                                                   
         save_id = results['related_id']                                                                                                 
         save_db = results['db_name']                                                                                                    
         save_value = results['value']
         temp = Dbtable.objects.filter(id=save_id).update(save_db=save_value)

How can I specify the specific table element to update based on save_db without hardcoding the database row name. I have a table in my database named.

I tried doing something like like:

Dbtable.objects.filter(id=save_id).update("%s"=save_value) % save_db    

but that failed spectacularly. Does anyone have an idea of how I can make this work?

4

1 に答える 1

2

キーワード引数のアンパッキングを使用できます:

Dbtable.objects.filter(id=save_id).update(**{save_db: save_value}) 

例:

>>> def test(a,b):
...     return a + b
... 
>>> test(**{'a': 1, 'b': 2})
3
于 2013-09-06T22:49:21.677 に答える