【问题标题】:Related Field got invalid lookup: icontains Django相关字段查找无效:​​icontains Django
【发布时间】:2021-02-06 11:14:37
【问题描述】:

所以我正在尝试创建一个搜索选项,用户可以通过类别和名称进行搜索,但只有名称有效,当我在类别上使用 icontains 时,它只是给了我一个错误,但没有给我一个错误,名称也只是类别不会在 HTML 中加载。类别应该在<select> 标签中加载,但它不起作用,searchall 函数处理我正在谈论的页面,search.html 是页面。

这是完整的错误

views.py

def searchall(request):
    getobject = request.GET['search']
    getcategory = request.GET['category']
    if getcategory == 'All':
        getcategory = ""
    search = Book.objects.filter(name__icontains=getobject, category__icontains=getcategory)
    oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
    lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
    hehe = CartItem.objects.filter(user=request.user)
    category = Category.objects.all()

    fianlprice = 0
    for item in hehe:
        fianlprice += item.book.price
    books = Book.objects.all()
    return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})

搜索.html

<h1>search</h1>
<h1>{{ error }}</h1>
<h1>Your cart currently costs ${{ price }}</h1>
<form method="GET" action="">
    <input type="text" placeholder="Search here" name="search" id="search">
    <button type="submit">Search</button>
</form>
{% for book in books %}
<h3>{{ book.name }}</h3>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
{% if book.id in cart %}
        <form method="POST" action="/removefromcartforproducts/">
            {% csrf_token %}
            <button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
        </form>
        {% elif book.id in order %}
        <h3>You already own this</h3>
        {% else %}
        <form method="POST" action="/addtocartforproducts/">
            {% csrf_token %}
            <button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
        </form>
    {% endif %}
{% endfor %}

models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'


class Book(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    image = models.ImageField()
    price = models.IntegerField()
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.name

class OrderItem(models.Model):
    order_id = models.CharField(max_length=10)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.user.username

class CartItem(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.user.username

【问题讨论】:

  • 如果您显示完整的回溯错误会很有帮助。
  • 谢谢!我添加了完整的错误

标签: python django


【解决方案1】:

在您的 searchall 方法中,您正在尝试过滤类别。但是,由于您的 Book 模型具有 category 属性,它是 object 不是字符串。因此,您必须深入其中并比较 namecategory

所以:

def searchall(request):
    getobject = request.GET['search']
    getcategory = request.GET['category']
    if getcategory == 'All':
        getcategory = ""
    search = Book.objects.filter(name__icontains=getobject, category__name__icontains=getcategory)
    oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
    lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
    hehe = CartItem.objects.filter(user=request.user)
    category = Category.objects.all()

    fianlprice = 0
    for item in hehe:
        fianlprice += item.book.price
    books = Book.objects.all()
    return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})

Refs 进行跨度关系查找 (__)。

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多