如何在 Django 上使用查询集进行搜索
How to search using queryset on Django
我有主题模型和 Post 模型。
我想使用查询集进行搜索。
当 q 有字符串时它工作得很好,但是当 q = '' (这意味着 All )它不显示所有 post.
我该如何解决这个问题?
models.py
class Topic(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User,on_delete=models.CASCADE)
topic = models.ForeignKey(Topic,on_delete=models.SET_NULL,null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail',kwargs={'pk':self.pk})
views.py
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context["topics"] = Topic.objects.all()
return context
def get_queryset(self):
if self.request.GET.get('q') != None:
q = self.request.GET.get('q')
else:
q = ''
# q = self.request.GET.get('q') if self.request.GET.get('q') != None else ''
return Post.objects.filter(topic__name__icontains=q).order_by('-date_posted')
你检查真实性 return Post.objects.none()
以防 q
真实 False
:
def get_queryset(self):
q = self.request.GET.get('q')
if q:
return Post.objects.filter(topic__name__icontains=q).order_by('-date_posted')
else:
return Post.objects<strong>.none()</strong>
也许可以将 PostListView
中的 get_queryset
方法更改为:
def get_queryset(self):
q_param = self.request.GET.get('q')
if q_param:
return Post.objects.filter(topic__name__icontains=q_param).order_by('-date_posted')
# if q is empty this will return all objects
return Post.objects.all().order_by('-date_posted')
解决了您的问题。
试试这个
q = request.GET.get('search')
if q is not None and q != u"":
q = request.GET.get('search')
Post.objects.filter(topic__name__icontains=q).order_by('date_posted')
else:
Post.objects.all().order_by('date_posted')
我有主题模型和 Post 模型。
我想使用查询集进行搜索。 当 q 有字符串时它工作得很好,但是当 q = '' (这意味着 All )它不显示所有 post.
我该如何解决这个问题?
models.py
class Topic(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User,on_delete=models.CASCADE)
topic = models.ForeignKey(Topic,on_delete=models.SET_NULL,null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail',kwargs={'pk':self.pk})
views.py
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
context["topics"] = Topic.objects.all()
return context
def get_queryset(self):
if self.request.GET.get('q') != None:
q = self.request.GET.get('q')
else:
q = ''
# q = self.request.GET.get('q') if self.request.GET.get('q') != None else ''
return Post.objects.filter(topic__name__icontains=q).order_by('-date_posted')
你检查真实性 return Post.objects.none()
以防 q
真实 False
:
def get_queryset(self):
q = self.request.GET.get('q')
if q:
return Post.objects.filter(topic__name__icontains=q).order_by('-date_posted')
else:
return Post.objects<strong>.none()</strong>
也许可以将 PostListView
中的 get_queryset
方法更改为:
def get_queryset(self):
q_param = self.request.GET.get('q')
if q_param:
return Post.objects.filter(topic__name__icontains=q_param).order_by('-date_posted')
# if q is empty this will return all objects
return Post.objects.all().order_by('-date_posted')
解决了您的问题。
试试这个
q = request.GET.get('search')
if q is not None and q != u"":
q = request.GET.get('search')
Post.objects.filter(topic__name__icontains=q).order_by('date_posted')
else:
Post.objects.all().order_by('date_posted')