Arpit Linux
Arpit Linux

Reputation: 259

Related Field got invalid lookup: icontains Django

So I'm trying to make an search option where users can search via categories and name but only name worked, when i used icontains on category it just gave me an error but did not give me an error with name also categories just does not load in HTML. Categories are supposed to load in <select> tag but it is not working, searchall function handles the page I'm talking about and search.html is the page.

this is the complete error enter image description here

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})

search.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

Upvotes: 2

Views: 1879

Answers (1)

Biplove Lamichhane
Biplove Lamichhane

Reputation: 4095

In your searchall method, you are trying to filter category. But, as your Book model have category property which is object not a string. So, you have to go further inside it and compare the name of that category.

So:

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 on span relationship lookups(__).

Upvotes: 1

Related Questions