跳至内容
sdvcrx's wiki
用户工具
登录
站点工具
搜索
工具
显示页面
过去修订
Export to Markdown
反向链接
最近更改
媒体管理器
网站地图
登录
>
最近更改
媒体管理器
网站地图
您的足迹:
django
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== Django ====== [[https://www.djangoproject.com/|Django]] is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. ===== View ===== ===== Model ===== ==== select_related ==== ''select_related'' 用于解决ORM中的n+1问题(即在包含外键的对象中执行查询时,会查询n+1次)。 假设有数据库模型如下: <code python models.py> class User(models.Model): name = models.CharField(max_length=255) class Article(models.Model): title = models.CharField(max_length=255) content = models.TextField() creator = models.ForeignField(User) </code> 在一个列表中,**简单地** 查询文章列表及其作者,将会查询n+1次: <code html index.html> {% for article in articles %} <h2>{{ article.title }}</h2> <p>author: {{ article.creator.name }}</p> {% endfor %} </code> 然而使用 ''SQL'' 语言使用 ''inner join'' 只需要查询一次即可。 Django orm 使用 ''select_related'' 来解决这个问题: <code python> articles = Article.objects.all().select_related('creator') </code> ==== Null && Blank ==== * Null: It is database-related. Defines if a given database column will accept null values or not. * Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid(). From [[https://simpleisbetterthancomplex.com/tips/2016/07/25/django-tip-8-blank-or-null.html|Django Tips #8 Blank or Null?]] ===== Template ===== ===== Forms =====
django.txt
· 最后更改: 2023/12/03 10:24 由
127.0.0.1
页面工具
显示页面
过去修订
反向链接
Export to Markdown
回到顶部