在Django里查询数据库时,如何按照desc倒序返回数据?
按照entry_date从小到大查询数据,可以写成:
Content.objects.order_by('entry_date')
从大到小排序:
Content.objects.order_by('-entry_date')
下面介绍其他种类的排序
随机排序:
Content.objects.order_by('?')
但是order_by(?)这种方式也许expensive并且slow,这取决于后端数据库。
按照关系表的字段排序
class Category(Base):
code = models.CharField(primary_key=True,max_length=100)
title = models.CharField(max_length = 255)
class Content(Base):
title = models.CharField(max_length=255)
description = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
# 按照Category的字段code,对Content进行排序,只需要外键后加双下划线
Content.objects.order_by('category__title')
# 如果只是按照外键来排序,会默认按照关联的表的主键排序
Content.objects.order_by('category')
# 上面等价于
Content.objects.order_by('category__code')
# 双下划线返回的是join后的结果集,而单下划线返回的是单个表的集合
Content.objects.order_by('category_title')
Note: 无论是单下划线还是双下划线,我们都可用{{ content.category.title }}在前端获取到关联表的数据。
在Django里查询数据库时,如何按照desc倒序返回数据?的更多相关文章
- Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?'
Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?' 一.原因 因为Python版本问题,MySQLdb ...
- PHP查询数据库较慢,nginx 超时 返回 504:Sorry, the page you are looking for is currently unavailable.
现象: PHP查询数据库较慢,大约 60s 后 nginx 返回 504:Sorry, the page you are looking for is currently unavailable. 检 ...
- ORM框架查询数据库时返回指定的字段
django model.objects.filter() 查询指定字段 1.model.objects.filter().values('field_name'),单个字段 2.model.obje ...
- java dbutils查询数据库时无法给部分字段赋值原因
1,javaBean如下: public class User { /** * 用户唯一标识(ID) */ private String uid; /** ...
- 查询数据库时mapper报错:It's likely that neither a Result Type nor a Result Map was specified.
因为mapper.xml里把resultType写成了parameterType
- django在读取数据库时未筛选到符合条件的记录会报错
(1)报错情况如下: DoesNotExist: Publisher matching query does not exist. (2)处理方法: try: p = Publisher.o ...
- Django项目配置数据库时,已安装mysqlclient,却提示 Did you install mysqlclient错误,后右报错ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
错误信息如下: 解决方案是: 找到自己的项目文件夹下的__init__.py 添加如下代码 解决这个问题后,右报错django2.2/mysql ImproperlyConfigured: mysq ...
- andriod前端传来经度 纬度 坐标 来查询数据库坐标周围500M内的类数据
@Transient public static List<Article> queryByPosition(PositionInfo pinfo){ //System.out.print ...
- 查询数据库中的表格---通过构造方法将数据存入到List集合中---遍历进行输出
package cn.jy.demo; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Res ...
随机推荐
- SET XACT_ABORT 的用法[转]
SET XACT_ABORT指定当 Transact-SQL 语句产生运行时错误时,Microsoft® SQL Server™ 是否自动回滚当前事务. 语法 SET XACT_ABORT { ON ...
- vimdiff: 使用Vim中强大的文件diff功能[转]
学习了一段时间的Vim,直到最近才发现Vim的diff功能是如此方便,对比代码变化再也不用到处去找diff软件或者依靠版本控制的diff了.强大的Vim. 下图是我在macVim中的diff效果. 下 ...
- [Head First Python]4. summary
1- strip()方法可以从字符串去除不想要的空白符 (role, line_spoken) = each_line.split(":", 1) line_spoken = li ...
- .net 拉姆达 groupby(p => p.X) order by count(c.Count())
//国家 var entityCountriesList = aliexpressEntities.SYS_CourierCode.Where(whereSelect.Compile()).Group ...
- 使用ARM模板部署自动扩展的Linux VMSS(2)
12.准备完了模板文件,我们使用Powershell来创建VMSS for Linux的自动扩展集合,首先登陆到Azure中国的ARM账号: Login-AzureRmAccount -Environ ...
- LeetCode_Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【Xamarin挖墙脚系列:Xamarin的终极破解步骤(更新)】
前面文章中,我们可以找到对应版本的补丁. Xamarin的 4.0.1717 版本,在补丁的地址中,有作者整理的全部的安装包.迅雷磁力贴: magnet:?xt=urn:btih:9FD298AA61 ...
- selenium webdriver使用过程中出现Element is not currently visible and so may not be interacted with的处理方法
参考文章: http://blog.csdn.net/passionboyxie/article/details/28661107 http://www.spasvo.com/ceshi/open/k ...
- UESTC_贪吃蛇 CDOJ 709
相信大家都玩过贪吃蛇游戏吧. 在n×m的迷宫中,有着一条长度不超过9的贪吃蛇,它已经将所有的食物吃光了,现在的目标是移动到出口. 它走的时候不能碰到自己的身体,也不能碰到墙壁.(如果贪吃蛇的长度> ...
- Word Search 解答
Question Given a 2D board and a word, find if the word exists in the grid. The word can be construct ...