2017-08-17 108 views
1

我的代碼有問題。我需要看起來像這樣一個模型的實例:如何在Django中動態修改數據庫請求?

class Example(models.Model): 
    # ... 
    foo = models.JSONField(default={}) 

,並根據用戶的輸入(JSON鍵)。例如:

Example.objects.filter(foo__userinput = bar) 

我該如何設法不使JSON密鑰硬編碼?

回答

0

您可以通過字典嘗試過濾

key = 'foo__%s' % userinput 
qfilter = {key: bar} 
Example.objects.filter(**qfilter) 
+0

非常感謝,它的工作:d – z0idb3rg