Django version=1.8 , IDE=pycharm, python 2.7
I have a search form and I want to search and list items("products") in model based on string matching product title or product description or product price.
Below is my "searchitems" part inside views.py . Also im confused what does the line below do in get_queryset function. Cheers
qs = super(ProductListView, self).get_queryset(*args,**kwargs)
#
# Search inside model function
def get_queryset(self, *args, **kwargs):
qs = super(ProductListView, self).get_queryset(*args,**kwargs)
query = self.request.GET.get("q")
if query:
qs = self.model.objects.filter(
Q(title__icontains=query) |
Q(description__icontains=query) |
Q(price=query)
)
return qs
class ProductListView(ListView):
model = Product
queryset=Product.objects.all() #no need to define this as it is a default
def get_context_data(self, *args, **kwargs):
context = super(ProductListView, self).get_context_data(*args, **kwargs)
return context
Below is models.py
from django.db import models
from django.core.urlresolvers import reverse
from django.db.models.signals import post_save
from django.utils.text import slugify
# Create your models here.
class ProductQuerySet(models.query.QuerySet):
def active(self):
return self.filter(active=True)
class ProductManager(models.Manager):
def get_queryset(self):
return ProductQuerySet(self.model, using=self.db)
def all(self, *args, **kwargs):
return self.get_queryset().active()
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10)
active = models.BooleanField(default=True)
objects = ProductManager()
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse("product_detail", kwargs={"pk": self.pk})
# OR use this- return "/product/%s"%(self.pk)
class Variation(models.Model):
product = models.ForeignKey(Product) ##this means each Variation is related to single product
title = models.CharField(max_length=120)
price = models.DecimalField(decimal_places=2, max_digits=10)
sale_price = models.DecimalField(decimal_places=2, max_digits=10, null=True, blank=True)
active = models.BooleanField(default=True)
inventory = models.IntegerField(null=True, blank=True) # default=-1 means unlimited
def __unicode__(self):
return self.title
def get_price(self):
if self.sale_price is not None:
return self.sale_price
else:
return self.price
def get_absolute_url(self):
return self.product.get_absolute_url()
# for post save receiver
def product_saved_receiver(sender, instance, created, *args, **kwargs):
# sender=modelclass, instance=actual instance being saved,created=boolean true if record was created
product = instance
variations = product.variation_set.all()
if variations.count() == 0:
new_var = Variation()
new_var.product = product
new_var.title = "Default"
new_var.price = product.price
new_var.save()
post_save.connect(product_saved_receiver, sender=Product)
# product image
# you need to install python pillow library to support.
# it checks if file uploaded is actually an image and checks extension
# class ProductImage(models.Model):
# product = models.ForeignKey(Product)
# image = models.ImageField(upload_to='products/') #image will be uploaded to media/mediaroot/products
#
# def __unicode__(self):
# return self.product.title
#slugify
def image_upload_to(instance, filename):
title = instance.product.title
slug = slugify(title)
file_extension = filename.split(".")[1]
# or basename,file_extension = filename.split(".")
new_filename = "%s.%s" %(instance.id,file_extension)
return "products/%s/%s" %(slug, filename)
# above function changed for slugfying
class ProductImage(models.Model):
product = models.ForeignKey(Product)
image = models.ImageField(upload_to=image_upload_to) #image will be uploaded to media/mediaroot/products
def __unicode__(self):
return self.product.title
With above codes i can search and list according to price eg. 50 or 67.89 but cannot search for strings and get below error
http://127.0.0.1:8000/products/?q=eric clapton riding with the king
ValidationError at /products/
[u"'eric clapton riding with the king' value must be a decimal number."]
Request Method: GET
Request URL: http://127.0.0.1:8000/products/?q=eric%20clapton%20riding%20with%20the%20king
Django Version: 1.8.4
Exception Type: ValidationError
Exception Value:
[u"'eric clapton riding with the king' value must be a decimal number."]
Exception Location: C:\Anaconda\lib\site-packages\django\db\models\fields\__init__.py in to_python, line 1602
Python Executable: C:\Anaconda\python.exe
Python Version: 2.7.10
Since the price requires a decimal value we should supply it a decimal value. Try the following view:
def get_queryset(self, *args, **kwargs):
qs = super(ProductListView, self).get_queryset(*args,**kwargs)
query = self.request.GET.get("q", False) # provide default value or you get a KeyError
if query:
filter_arg = Q(title__icontains=query) | Q(description__icontains=query)
try:
filter_arg |= Q(price=float(query))
except ValueError:
pass
qs = self.model.objects.filter(filter_arg)
return qs
qs = super(ProductListView, self).get_queryset(*args,**kwargs)
This is used to obtain the queryset provided by the parent classes of our view class ProductListView
. Look in to python classes and inheritance here:
http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
filter_arg |= Q(price=float(query))
this is used to append to our filter_arg value. It's the same as filter_arg = filter_arg | Q(price=float(query)
float(query)
with this we are trying to convert the query variable to a float and we put this in a try
statement because it could give us a ValueError
in which case the query
value is not a float
.