departments=models.Department.objects.filter(dpm_status=1).values_list('dnp__name') print(departments ) # queryset中是一个个元组。“departments”:[("运输部门",),("仓储部门",)] 3.values_list(flat=True) 1 2 departments=models.Department.objects.filter(dpm_status=1).values_list('dpm_name', flat=True) print(departmen...
2.values_list() 1 2 print(Question.objects.values_list('title'))# 等到是一个元组 <QuestionQuerySet [('查询优化之select_related与prefetch_related - 简书',), ('你们都是怎么学 Python 的?',)]> 3.values_list(flat=True) 1 2 print(Question.objects.values_list('title', flat=True))# 得...
Entry.objects.values_list('id', flat=True) <QuerySet [1, 2]> 这种方法比较好用,所以我常常在项目中使用。 4、distinct() 相当于 mysql 的 DISTINCT 的用法,这个用法需要用到前面介绍的 values() 方法。 使用方法如下: Blog.objects.values("name").distinct() 5、using() 有时候,我们在 Django 项目...
values_list() 函数返回的则是元组列表,效果如下: Entry.objects.values_list('id','headline')<QuerySet[(1,'123'),(2,'wwwqeq')]> 1. 2. 如果我们需要获取的仅有一个字段,返回的也是一个元组列表,但是为了方便,我们也可以变成列表格式,加上 flat 参数即可。 注意:这种情况仅存在于所需要获取的字段为...
️最终修改 修改很简单,直接把ValuesListQuerySet强转成list就可以了,如下: ids_list = list(Products.objects.filter(ProductClass_id=51,status=200).values_list("id",flat=True)) 这个坑就在于我一直以为values_list再加上flat=true,返回类型就是list,还有在20个值以下,print出来的效果也和list一样. ...
The documentation should be updated to clarify that the structure returned by a values_list queryset with flat=True is not a plain list, but a ValuesListQuerySet. Alternatively, the code should actually output a list. I spent a while struggling to understand why I couldn't use the Python ...
values_select, *query.annotation_select] tuple_class = self.create_namedtuple_class(*names) new = tuple.__new__ for row in super().__iter__(): yield new(tuple_class, row) class FlatValuesListIterable(BaseIterable): """ Iterable returned by QuerySet.values_list(flat=True) that yields...
objects.values_list('title', flat=True) return render(request, 'music_list.html', {'music_list': music_list}) 第三步:设计模板模板是用于呈现数据的HTML文件。在Django中,模板文件通常放置在templates目录下。我们将创建一个简单的模板,用于显示音乐列表。 <!-- 在 templates/music_list.html 文件中 -...
values_list("pk", flat=True) ct = ContentType.objects.get_for_model(queryset.model) return HttpResponseRedirect( "/export/?ct=%s&ids=%s" % ( ct.pk, ",".join(str(pk) for pk in selected), ) ) As you can see, the action is rather short; all the complex logic would belong ...
values_list('id', flat=True) 在上面的示例中,我们首先从myapp.models模块中导入MyModel模型。然后,我们创建一个QuerySet对象,该对象包含MyModel模型的所有实例。接下来,我们定义一个额外的SQL语句extra_sql,其中包含自定义排序的ORDER BY子句。最后,我们使用extra()方法将额外的SQL语句添加到查询中,并将结果存储...