How can filter by list in django

2024/11/20 19:25:08

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_response132.                     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_view58.         return view_func(*args, **kwargs)
File "/home/cp/Documents/myshopup/markatix/customapi/vendors.py" in CustomerRequirements365.      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_method127.                 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 filter679.         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_exclude697.             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_q1310.         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_q1338.                     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_filter1209.             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_lookup1102.                 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_lookup143.         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_lookup729.             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_value985.         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.

Answer

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 )
https://en.xdnf.cn/q/119796.html

Related Q&A

Python: get the return code of ant sub-process in windows

I use python to call ant, I want to get the return code of the ant for detect ant error.for example, in cmd.exe, C:\Documents and Settings\Administrator>ant sfsf Buildfile: build.xml does not exist!…

Skipp the error while scraping a list of urls form a csv

I managed to scrape a list of urls from a CSV file, but I got a problem, the scraping stops when it hits a broken link. Also it prints a lot of None lines, is it possible to get rid of them ? Would ap…

Getting the TypeError - int object is not callable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Reordering columns in CSV

Question has been posted before but the requirements were not properly conveyed. I have a csv file with more than 1000 columns:A B C D .... X Y Z 1 0 0.5 5 .... 1 7 6 2 0 0.6 4 …

Variable not defined in while loop in python?

I am trying to write a simple program in python to read command line arguments and print a final word based on the arguments. If there is any argument of the form "-f=" then the will go to t…

Hours and time converting to a certain format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Python socket server: listening to multiple clients [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

I have a problem with encoding with russian language for my python script [duplicate]

This question already has answers here:UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)(34 answers)Closed last year.I am trying to send an email fro…

how do you style data frame in Pandas

I have this data frame: dfServer Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc.…

Vacation price program Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…