如何使用python在sqlite3中存储和检索字典

时间:2013-11-11 14:29:14

标签: python django

我从查询对象获取此类型的结果:

report_qs = CustomReport.objects.all().filter(id=report_id)

[<CustomReport: {'Name': 'Zara', 'Age': 7, 'Class': 'First'}>]

CustomReport模型:

class CustomReport(models.Model):
    """
        Save the result of the custom report form wizard for further
        regeneration. The result is saved as a serialized dictionary.
    """

    class Meta:
        unique_together = (('creation_date', 'name'),)

    name = models.CharField(max_length=64)
    query = models.TextField()
    creation_date = models.DateTimeField(default=datetime.datetime.now,
                                         editable=False)


    def __unicode__(self):
        return self.query

如何获取值和键?

1 个答案:

答案 0 :(得分:0)

你应该试试

report_qs = CustomReport.objects.all().filter(id=report_id).values('name', 'age', 'class')

现在查看report_qs,您可以轻松获取值和键。 您可以迭代report_qs

for obj in report_qs:
    print obj.keys()
    print obj.values()