标签:lte tps 这一 from UNC cascade 你好 HERE 链式
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'category'
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True)
create_time = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)
class Meta:
db_table = 'article'
from django.http import HttpResponse
from .models import Article
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
def index4(request):
articles = Article.objects.filter(id__gte=2).order_by("create_time")
for article in articles:
print("%s, %s, %s" % (article.title, article.content, article.create_time))
print(articles.query)
return HttpResponse("success !")
3, 钢铁是怎样炼成的, 你好, 2020-02-05 03:03:30.860556+00:00
2, Hello World, 大家好, 2020-02-05 03:04:59.860556+00:00
4, 中国吸引力, 精彩极了, 2020-02-05 03:04:59.860556+00:00
由输出的结果,我们可以看出文章已经按时间的顺序进行排序了,默认情况下是按照降序的顺序。
SELECT article
.id
, article
.title
, article
.content
, article
.category_id
, article
.create_time
FROM article
WHERE article
.id
>= 2 ORDER BY article
.create_time
ASC。
可以看出我们的查询条件已经被翻译成了WHERE article
.id
>= 2 ORDER BY article
.create_time
ASC
from django.db.models import Q
from django.http import HttpResponse
from .models import Article, Category
from django.db.models.query import QuerySet
from django.db.models.manager import Manager
def index5(request):
articles = Article.objects.filter(id__gte=3).filter(~Q(id=4))
# articles = Article.objects.filter(id__gte=3).exclude(id=4)
for article in articles:
print("%s, %s, %s" % (article.id, article.title, article.create_time))
print(articles.query)
return HttpResponse('success!')
3, 钢铁是怎样炼成的, 2020-02-05 03:03:30.860556+00:00
执行的sql语句为:SELECT article
.id
, article
.title
, article
.content
, article
.category_id
, article
.create_time
FROM article
WHERE (article
.id
>= 3 AND NOT (article
.id
= 4))
def index5(request):
articles = Article.objects.exclude(title__icontains='hello')
for article in articles:
print("%s, %s, %s" % (article.id, article.title, article.create_time))
print(articles.query)
return HttpResponse('success!')
3, 钢铁是怎样炼成的, 2020-02-05 03:03:30.860556+00:00
4, 中国吸引力, 2020-02-05 03:04:59.860556+00:00
django底层执行的sql语句:SELECT article
.id
, article
.title
, article
.content
, article
.category_id
, article
.create_time
FROM article
WHERE NOT (article
.title
LIKE %hello%)
def index5(request):
articles = Article.objects.annotate(category_name=F("category__name"))
for article in articles:
print("%s, %s, %s" % (article.id, article.title, article.category_name))
print(articles.query)
return HttpResponse('success!')
1, Hello, 最新文章
2, Hello World, 最热文章
3, 钢铁是怎样炼成的, 高评分文章
4, 中国吸引力, 高评分文章
SELECT article
.id
, article
.title
, article
.content
, article
.category_id
, article
.create_time
, category
.name
AS category_name
FROM article
LEFT OUTER JOIN category
ON (article
.category_id
= category
.id
)
79.常用的返回QuerySet对象的方法使用详解: filter, exclude,annotate
标签:lte tps 这一 from UNC cascade 你好 HERE 链式
原文地址:https://www.cnblogs.com/guyan-2020/p/12267195.html