Python:在元组列表中转换查询集 [英] Python: Converting a queryset in a list of tuples

查看:90
本文介绍了Python:在元组列表中转换查询集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class User(models.Model):
    email = models.EmailField()
    name = models.CharField()

如何获取电子邮件和用户名作为元组列表?我当前的解决方案是:

How to get email and name of User as a list of tuples? My current solution is this:

result = []
for user in User.objects.all():
    result.append((user.email, user.name))


推荐答案

如果这是django ORM查询集(或其结果),则可以使用 values_list 方法代替 values code>。这将完全满足您的需求。

If this is an django ORM queryset (or result of it), you can just use values_list method instead of values. That will give exactly what you want.

这篇关于Python:在元组列表中转换查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆