我有一个 JSON 变量,例如:
myjson = [{"name":"bla", "value":111}, {"name":"ble", "value":222}, {"name":"bli", "value":333}, {"name":"blo", "value":444}]
我的 Django 模型是:
class myTable(models.Model):
name = models.TextField(max_length=2000)
bla = models.IntegerField(default=0)
ble = models.IntegerField(default=0)
bli = models.IntegerField(default=0)
blo = models.IntegerField(default=0)
def __unicode__(self):
return '%s, %s, %s, %s' % (self.name, self.bla, self.ble, self.bli, self.blo)
我想保存这一行(来自我的views.py):
from myproject.models import myTable
n = "lalala"
query = myTable(name="lalala", bla=111, ble=222, bli=333, blo=444)
query.save()
我不知道如何构建迭代 JSON 的查询。
如果我建立一个字符串:
#>>> q="lalala"
#>>> myzonesstring = 'name="'+q+'", '
#>>> for m in myjson:
#... myzonesstring += str(m["name"])+'='+str(m["value"])+', ' ...
#>>> myzonesstring 'name="lalala", bla=111, ble=222, bli=333, blo=444, '
#>>> myzonesstring[:-2] 'name="lalala", bla=111, ble=222, bli=333, blo=444'
#>>> query = myTable(myzonesstring[:-2])
#>>> query.save() [. python . ValueError: invalid literal for int() with base 10: 'name="lalala", bla=111, ble=222, bli=333, blo=444'