python - 如何对表中的一列 Django 元素进行排序?

标签 python django

如何对表格中的一列 Django 元素进行排序?

我注意到 Django 管理员执行此操作。怎么办?

是否有任何可供我阅读的网络资源?

*我所说的“Django 元素”指的是看起来像这样的可插入模板对象:

{{ x.y }}

最佳答案

值得一看 django 管理中的 ChangeList View 源代码:

http://code.djangoproject.com/svn/django/branches/releases/1.2.X/django/contrib/admin/views/main.py

订购流程大致是:

  1. 您单击管理中的列标题,这将重新加载带有 url 中相关订单参数的页面,可在 request.GET 中使用

  2. 此订单参数在 ChangeList View 中解析:

    self.order_field, self.order_type = self.get_ordering()

  3. 生成查询集:

    self.query_set = self.get_query_set() 特别是函数中的以下几行: 如果 self.order_field: qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field))

在我看来,在理解源代码的同时使用 django admin 是学习 django 的最佳方式之一!

关于python - 如何对表中的一列 Django 元素进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4939866/

相关文章:

python - 在shell脚本中捕获python脚本抛出的异常

python - 从 postgreSQL 数据库(Django 站点)备份所有数据

python - AJAX 中 CSRF token 缺失或不正确

python - 处理丢失数据时出现 IndexError : too many indices for array ,

python - Scrapy 错误 : exceptions. AttributeError: 'HtmlResponse' 对象没有属性 'urljoin'

python - 如何从数据框中删除表情符号?

python - 返回不在括号内的字符串的索引

python - 如何缩小 Django 查询集中的结果范围?

ruby-on-rails - 特征区分: Rails/Django

python - 为 mysql-python (MySQLdb) 更改 django 中的 CLIENT_FOUND_ROWS 标志?