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:00python
由輸出的結果,咱們能夠看出文章已經按時間的順序進行排序了,默認狀況下是按照降序的順序。sql
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:00django
執行的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%)code
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, 中國吸引力, 高評分文章blog
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
)排序