这是我的字典
{u'krishna': [u'vijayawada', u'gudivada', u'avanigada']}
我想迭代项目并保存在数据库中,我的模型是
class Example(models.Model):
district = models.CharField(max_length=50,**optional)
taluk = models.CharField(max_length=20,**optional)
它应该保存为:
-----------------------------
|district | taluk |
|-----------|--------------- |
|krishna | vijayawada |
|-----------|----------------|
|krishna | gudivada |
|----------------------------|
|krishna | avanigada |
------------------------------
答案 0 :(得分:2)
您可以这样做:
form models import Example
places = {u'krishna': [u'vijayawada', u'gudivada', u'avanigada']}
for district in places:
for taluk in district:
e = Example(district=district, taluk=taluk)
e.save()
答案 1 :(得分:1)
for key in dict:
for value in dict[key]:
example = Example()
example.district = key
example.taluk = value
example.save()
答案 2 :(得分:0)
这对你有用: -
for districtName in places.keys():
for talukName in places[districtName]:
print districtName,talukName #Try to print it
addData = Example.objects.create(district=districtName,taluk=talukName)
addData.save()