如何使用 Q 对象在 django 模型中查询浮点/整数值或字符串?
Posted
技术标签:
【中文标题】如何使用 Q 对象在 django 模型中查询浮点/整数值或字符串?【英文标题】:How can I query for float/int value or string in a django model using Q Objects? 【发布时间】:2016-01-08 03:40:52 【问题描述】:Django 版本=1.8,IDE=pycharm,python 2.7
我有一个搜索表单,我想根据字符串匹配产品标题或产品描述或产品价格来搜索和列出模型中的项目(“产品”)。
下面是我在 views.py 中的“searchitems”部分。我也很困惑下面的行在 get_queryset 函数中做了什么。干杯
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
下面是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
使用上面的代码,我可以根据价格搜索和列出,例如。 50 或 67.89 但无法搜索字符串并低于错误
http://127.0.0.1:8000/products/?q=eric克莱普顿与国王一起骑行
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
【问题讨论】:
【参考方案1】:由于价格需要十进制值,我们应该为其提供十进制值。试试下面的视图:
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)
这个是用来获取我们的视图类ProductListView
的父类提供的查询集。在这里查看 python 类和继承:
http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
filter_arg |= Q(price=float(query))
这用于附加到我们的 filter_arg 值。同filter_arg = filter_arg | Q(price=float(query)
float(query)
有了这个,我们试图将查询变量转换为浮点数,我们把它放在try
语句中,因为它可以给我们一个ValueError
,在这种情况下query
值不是@ 987654331@.
【讨论】:
感谢@Bogdan 立即起作用。我很困惑 qs = super(ProductListView, self).get_queryset(*args,**kwargs) 和 filter_arg |= Q(price=float(query)) 在这里做了什么......你能提供更多信息或链接吗?格拉西亚斯 :)以上是关于如何使用 Q 对象在 django 模型中查询浮点/整数值或字符串?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Django Querysets 和 Q() 与相同模型类型的对象进行比较?