为什么我的搜索函数返回一个与错误的字段有关的查找无效:​​类别

发布于 2025-02-03 05:21:29 字数 1784 浏览 2 评论 0原文

我的搜索功能正常工作,但是每次我尝试在网站上搜索存在的帖子时,都会给我带来错误。当我在类别下的问题模型中删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2025-02-10 05:21:29

如果您使用category __…过滤,这意味着过滤器的其余部分应处理类别型号的字段,并且a category具有a <代码>名称字段,因此您使用以下方式过滤:

list_of_question = Question.objects.filter(
    Q(title__icontains=query) |
    Q(category__name__icontains=query)
)

If you filter with category__… this means that the rest of the filter should deal with fields of a Category model, and a Category has a name field, so you filter with:

list_of_question = Question.objects.filter(
    Q(title__icontains=query) |
    Q(category__name__icontains=query)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文