为什么我的搜索函数返回一个与错误的字段有关的查找无效:类别
我的搜索功能正常工作,但是每次我尝试在网站上搜索存在的帖子时,都会给我带来错误。当我在类别下的问题模型中删除 foreferkey 字段时,错误就消失了。请如何过滤具有 foreferkey的类别
错误:
FieldError at /index/
Related Field got invalid lookup: category
我的模型:
class Category(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
def __str__(self):
return str(self.name)
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=False, null=False)
body = RichTextField(blank=False, null=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Question, self).save(*args, **kwargs)
def __str__(self):
return str(self.title)
视图:
def index(request):
query = request.GET.get('q', None)
list_of_question = Question.objects.all()
if query is not None:
list_of_question = Question.objects.filter(
Q(title__icontains=query) |
Q(category__category__icontains=query)
)
unread_notifications = Notification.objects.filter(user=request.user, is_read=False).count()
paginator = Paginator(list_of_question, per_page=1)
return render(request, 'index.html', {'unread_notifications':unread_notifications,'list_of_question':list_of_question, 'paginator':paginator})
我的表单:
<form action="" method="GET">
<input placeholder="Search By Category" type="text" class="form-control" name="q">
</form>
My search function works fine but it throws me an error every time i try to search an existence post in my website. when i deleted ForeignKey in the Question model under category field the error is gone. please how can i filter that category that have a ForeignKey
the error:
FieldError at /index/
Related Field got invalid lookup: category
my models:
class Category(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
def __str__(self):
return str(self.name)
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=False, null=False)
body = RichTextField(blank=False, null=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Question, self).save(*args, **kwargs)
def __str__(self):
return str(self.title)
the views:
def index(request):
query = request.GET.get('q', None)
list_of_question = Question.objects.all()
if query is not None:
list_of_question = Question.objects.filter(
Q(title__icontains=query) |
Q(category__category__icontains=query)
)
unread_notifications = Notification.objects.filter(user=request.user, is_read=False).count()
paginator = Paginator(list_of_question, per_page=1)
return render(request, 'index.html', {'unread_notifications':unread_notifications,'list_of_question':list_of_question, 'paginator':paginator})
my form:
<form action="" method="GET">
<input placeholder="Search By Category" type="text" class="form-control" name="q">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
category __…
过滤,这意味着过滤器的其余部分应处理类别
型号的字段,并且acategory
具有a <代码>名称字段,因此您使用以下方式过滤:If you filter with
category__…
this means that the rest of the filter should deal with fields of aCategory
model, and aCategory
has aname
field, so you filter with: