Django 中 first()的意思

first():顾名思义,第一个。根据条件找到数据库中的符合条件的第一个。相当于.all()[0]

区别在于:
first():如果没有匹配到对象则为None。
all()[0]:如果没有匹配到,会报IndexError错误

例如:
Dynamic.objects.filter(song_id=int(id)).first()  找到符合条件的第一个,如果没有返回None
Dynamic.objects.filter(song_id=int(id)).all()[0]  如果没有,会报错

PS:

last():根据条件找到数据库中的符合条件的最后一个数据,相当于all()[-1]。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Django is a high-level Python web framework that is designed for rapid development and clean, pragmatic design. It follows the Model-View-Controller (MVC) architectural pattern and is built on top of the Python programming language. Django is known for its ability to create complex web applications quickly and efficiently. It comes with a wide range of built-in features, including an ORM (Object-Relational Mapping) system, a templating engine, authentication and authorization systems, and a powerful administration interface. To get started with Django, you will need to have Python installed on your computer. Once you have Python installed, you can install Django using pip, the Python package manager. After installing Django, you can create a new Django project using the following command: ``` django-admin startproject project_name ``` This will create a new Django project with the name you specified. You can then create new Django apps within your project using the following command: ``` python manage.py startapp app_name ``` This will create a new Django app within your project with the name you specified. From there, you can start building your Django application by defining your models, views, and templates. Django also comes with a powerful built-in development server, which you can start using the following command: ``` python manage.py runserver ``` This will start the development server on your local machine, and you can view your Django application by navigating to http://localhost:8000/ in your web browser. Overall, Django is a powerful and flexible web framework that can help you build complex web applications quickly and efficiently.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值