Py学习  »  问与答

Re:如何才能使queryset中字段的显示顺序跟model中定义的字段显示顺序一致?

django • 9 年前 • 3369 次点击  

问题描述:

>>> queryset_values = Student.objects.filter(pk=1).values()
>>> queryset_values

运行之后,字段显示的前后顺序完全不同与model中字段的先后顺序,请问如何保证显示的顺序与model相同?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/670
 
3369 次点击  
文章 [ 1 ]  |  最新文章 9 年前
Py站长
Reply   •   1 楼
Py站长    9 年前

queryset.values() returns a list of dicts, and dicts don't return their items in any particular order.

if you want your values in order, use queryset.values_list which returns a list of tuples.

(you will need to keep the list of columns in a separate context variable)

http://stackoverflow.com/questions/11168488/django-queryset-values-method-fields-order-is-different-with-returned