Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

python - How can filter by list in django

I am trying to filter a queryset by a list I am getting unicode data into format of 1,4,5,6 by

category = request.GET.getlist(category')
print type(category)



data = Leads.objects.filter(item_required__id= category ) 

then i am getting a error

invalid literal for int() with base 10: '1,4'

So how can i fix this.

Traceback:
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/home/cp/Documents/myshopup/markatix/customapi/vendors.py" in CustomerRequirements
  365.      cust_leads = CustomerLeads.objects.filter(item_required__id__in= category ).values('customer_name','budget','event','posting_date','quantity','other','fb_id','image','title').order_by('-posting_date')
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/query.py" in filter
  679.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  697.             clone.query.add_q(Q(*args, **kwargs))
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1310.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1338.                     allow_joins=allow_joins, split_subq=split_subq,
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1209.             condition = self.build_lookup(lookups, col, value)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_lookup
  1102.                 return final_lookup(lhs, rhs)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
  105.         self.rhs = self.get_prep_lookup()
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
  143.         return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  729.             return [self.get_prep_value(v) for v in value]
File "/home/cp/Documents/envMyShopUp/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  985.         return int(value)

Exception Type: ValueError at /customapi/vendor/customer-requirements/
Exception Value: invalid literal for int() with base 10: '1,4'

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I done some steps wrong.finally i got and found solution.

    category = request.GET.get('category')
l1=[]
    category_list =category.split(',')
    for i in category_list:
        a =int(i)
        l1.append(a)

cust_leads = CustomerLeads.objects.filter(item_required__id__in= l1 )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...