0
    @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 的新手。我没有使用 django 搜索或任何其他内置搜索模块。在搜索产品功能中,我无法过滤以产品字段为外键的 kitchenstylemapping 模型。任何帮助表示赞赏

4

1 回答 1

0

这个链接:Django Query Set API是你最好的新朋友。

Django 附带了很棒的文档,你应该充分利用它。例如,过滤器中的双下划线表示 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)了解它具有和可以查询的字段类型之类的东西,它们不会马上就都有意义,但这是一个很好的起点。

希望有帮助。

于 2013-07-31T11:16:30.660 回答