当前位置: 首页 > 工具软件 > Just ORM > 使用案例 >

orm查询条件

翟理
2023-12-01

1.exact相当于=号,iexact相当于like
def index(request):
# article = Article.objects.filter(title__exact=‘hello world’) #SELECT article_article.id, article_article.title, article_article.content, article_article.category_id, article_article.author_id FROM article_article WHERE article_article.title = hello world
article = Article.objects.filter(title__iexact=‘hello world’) #SELECT article_article.id, article_article.title, article_article.content, article_article.category_id, article_article.author_id FROM article_article WHERE article_article.title LIKE hello world
print(article.query)
print(article)
return HttpResponse(“success index”)

def index(request):
acticle = Article.objects.get(pk=1)
print(type(acticle)) #<class ‘article_test.models.Article’>,不能用query查询
return HttpResponse(“index1”)
def article_test(request):
article_test = Article.objects.filter(id__exact=1)
print(article_test.query)# QuerySet()模型
print(article_test)
return HttpResponse(“test”)

from django.db import models

Create your models here.

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)
class Meta:
db_table = ‘article’

def index2(request):
# result = Article.objects.filter(title__contains=‘Hello’)#标题包含hello,大小写敏感,若hello为小写则无法查询出来,title内容为"Hello world’
# result = Article.objects.filter(title__icontains=‘Hello’)#忽略大小写
# print(result.query)
# print(result)
# result = Article.objects.filter(id__in=[1,2,3]) #相当于mysql中in
# for i in result:
# print(i)
# categories = Category.objects.filter(article__id__in=[1,2,3]) #多表查询,根据category id查询article的表数据
# categories = Category.objects.filter(articles__id__in=[1,2,3]) #引用了反向查询,只能用articles
# for i in categories:
# print("=====>",i)
# articles = Article.objects.filter(title__icontains=‘hello’)
# categories = Category.objects.filter(articles__in=articles)#SELECT category.id, category.name FROM category INNER JOIN article ON (category.id = article.category_id) WHERE article.id IN (SELECT U0.id FROM article U0 WHERE U0.title LIKE %hello%)
# for i in categories:
# print(i)
# print(categories.query)
# caticle = Article.objects.filter(id__gt = 2) #id大小2
# caticle = Article.objects.filter(id__gte = 2) #id大小等于2
# caticle = Article.objects.filter(id__lt = 2) #id小于2
# caticle = Article.objects.filter(id__lte = 2) #id小于等于2
# for i in caticle:
# print(i)
# caticle = Article.objects.filter(title__startswith=‘hello’)#以hello开始,大小写敏感 SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY hello%
# caticle = Article.objects.filter(title__istartswith=‘hello’)#以hello开始,忽略大小写 SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE hello%
# caticle = Article.objects.filter(title__iendswith=‘hello’) #同istartswitch相反
# print(caticle.query)
# print(caticle)
# article = Article.objects.all()
# print(article.query)
# print(article)
# q = Article.objects.annotate(Count(‘title’))
# q = Question(question_text=“What’s new?”,pub_date = timezone.now())
# current_year = timezone.now().year
# q = Question.objects.get(pub_date__year=current_year)
# # q.question_text = “What’s up?”
# # q.save()
# print(current_year)
# q = Question.objects.get(pk=1)
# print(q.was_published_recently())
# q = Question.objects.get(pk=1)

# c = q.choice_set.create(choice_text=‘Just hacking again’, votes=0)

# return HttpResponse(“index2”)

latest_question_list = Question.objects.order_by("-pub_date")[:5]

output = ‘,’.join([q.question_text for q in latest_question_list])

return HttpResponse(output)

latest_question_list = Question.objects.order_by("-pub_date")[:5]

context = {

“latest_question_list”:latest_question_list

}

return render(request,‘index2.html’,context=context)

start_time = make_aware(datetime.datetime(year=2018,month=4,day=4,hour=17,minute=0,second=0))

end_time = make_aware(datetime.datetime(year=2018,month=4,day=4,hour=18,minute=0,second=0))

articles = Article.objects.filter(create_time__range=(start_time,end_time))

print(articles.query)

print(articles)

return HttpResponse(‘INDEX’)

#SELECT article.id, article.title, article.content, article.category_id, article.create_time FROM article WHERE article.create_time BETWEEN 2018-04-04 09:00:00 AND 2018-04-04 10:00:00
# articles = Article.objects.filter(create_time__date=datetime.datetime(year=2018,month=4,day=4))
# articles = Article.objects.filter(create_time__isnull=False)#不为空的数据
articles = Article.objects.filter(title__iregex=‘hello’)#匹配 hello,iregex忽略大小写 SELECT article.id, article.title, article.content, article.category_id, article.create_time FROM article WHERE article.title REGEXP hello
print(articles.query)
print(articles)
return HttpResponse(‘sss’)

 类似资料: