@login_required
def searchProduct(request):
"""
to search a particular product with name
"""
search_terms =''
if request.GET['search_term']:
search_terms = request.GET.get('search_term')
if search_terms:
products = models.Product.objects.filter(product_name__icontains=search_terms)
for product in products:
kitchenstyle = KicthenStyleMapping.objects.filter(product= product)
print kitchenstyle
return render_to_response('admin/product/searchListProduct.html',{'search_terms': search_terms,'products' : products }, context_instance=RequestContext(request))
return HttpResponseRedirect('/')
class KicthenStyleMapping(models.Model):
style_mapping_id = models.AutoField(primary_key=True)
style = models.ForeignKey(KitchenStyle)
product = models.ForeignKey(Product)
class Meta:
db_table = 'kicthen_style_mapping'
class KitchenStyle(models.Model):
id = models.AutoField(primary_key=True)
style_name = models.CharField(max_length=248L, blank=True)
description = models.TextField(blank=True)
estimated_time = models.CharField(max_length=100L, blank=True)
status = models.CharField(max_length=10L)
class Meta:
db_table = 'kitchen_style'
class Product(models.Model):
id = models.AutoField(primary_key=True)
cabinet = models.CharField(max_length=100L, blank=True)
material = models.ForeignKey(Materials)
category = models.ForeignKey(Category, null=True, blank=True)
sub_category = models.ForeignKey(SubCategory, null=True, blank=True)
product_name = models.CharField(max_length=135, blank=True)
product_code = models.CharField(max_length=135, blank=True)
short_description = models.TextField(blank=True)
descriptions = models.TextField(blank=True)
product_price = models.FloatField(null=True, blank=True)
min_height = models.FloatField(null=True, blank=True)
max_height = models.FloatField(null=True, blank=True)
height_scale = models.FloatField(null=True, blank=True)
min_width = models.FloatField(null=True, blank=True)
max_width = models.FloatField(null=True, blank=True)
width_scale = models.FloatField(null=True, blank=True)
min_depth = models.FloatField(null=True, blank=True)
max_depth = models.FloatField(null=True, blank=True)
depth_scale = models.FloatField(null=True, blank=True)
is_hinges = models.CharField(max_length=12)
image = models.CharField(max_length=135, blank=True)
is_door = models.CharField(max_length=12, blank=True)
discount = models.FloatField(null=True, blank=True)
is_drawer = models.CharField(max_length=12)
video_url = models.CharField(max_length=200L, blank=True)
is_custom = models.CharField(max_length=4L)
class Meta:
db_table = u'product'
我是django的新bie。我没有使用django搜索或任何其他内置的搜索模块。 这里的搜索产品功能我不能过滤使用产品领域作为外键的kitchenstylemapping模型。 任何帮助赞赏
答案 0 :(得分:0)
此链接:Django Query Set API是您最好的朋友。
Django附带了精彩的文档,您应该充分利用它。 E.g过滤器中的双下划线意味着Django可以查找外键;它很强大,但不是很漂亮。
我发现编写查询的最简单方法是打开一个shell:
python manage.py shell
from (your model file) myapp.models import *
KitchenStyle.objects.filter(style_set__product_name__icontains=searchterms)
然后了解过滤器语法。
使用诸如dir(your_object)
之类的东西来了解它所拥有的字段类型,并且可以查询它们,它们不会立即有意义,但它是一个很好的起点。
希望有所帮助。