python - django search function , models are not working -


    @login_required def searchproduct(request):     """     search particular product 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)             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' 

i new bie in django. not using django search or other inbuilt search modules. here in search product function cant filter kitchenstylemapping model product field foreign key. appreciated

this link: django query set api new best friend.

django comes fantastic documentation , should take full advantage of it. e.g double underscore in filter means django can lookup foreign keys; it's powerful, not pretty.

the easiest way i've found of writing queries open shell:

python manage.py shell (your model file) myapp.models import *  kitchenstyle.objects.filter(style_set__product_name__icontains=searchterms) 

and learn way around filter syntax.

use things dir(your_object) idea of kind of fields has , can queried, won't make sense right away, it's place start.

hope helps.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -