I have an eve app running on my mongodb collection col10. I am trying to get a response where I have multiple values selected from the same key, example:
http://127.0.0.1:4567/col10?where={"var0053":[1130,1113]}
## returns 0 objects
I have also tried:
http://127.0.0.1:4567/col10?where=var0053==[1130,1113]
## returns just the objects with var0053 = 1113
is there a way of requesting to server more than one value from the same key?
If you are using GET method your url should be looking like that :
http://IP_ADDRESS:8080/test?list=1&list=2&list=3
for retrieving it:
String[] arrlist=request.getParameterValues('list');
your array will be filled with separated values:
//["1","2","3"]
When you retrieving your list parameters it wouldn't be parsed as array but as a series of String which will be grouped later on into an array.
Which means even if you write it list[]=1&list[]=2&list[]=3, list[=1&list[=2&list[=3, list*=1&list*=2&list*=3 or list=1&list=2&list=3 it would always be giving you the same answer whether you retrieve it as
request.getParameterValues('list[]') //["1","2","3"]
request.getParameterValues('list[') //["1","2","3"]
request.getParameterValues('list*') //["1","2","3"]
request.getParameterValues('list') //["1","2","3"]
With Eve, you can use mongodb syntax for queries, like this:
http://127.0.0.1:4567/col10?where={"var0053": {"$in": ["1130", "1113"]}}
Documentation is here https://docs.python-eve.org/en/stable/features.html#filtering.
Related
In a Django view you can access the request.GET['variablename'], so in your view you can do something like this:
myvar = request.GET['myvar']
The actual request.GET['myvar'] object type is:
<class 'django.http.QueryDict'>
Now, if you want to pass multiple variables with the same parameter name, i.e:
http://example.com/blah/?myvar=123&myvar=567
You would like a python list returned for the parameter myvar, then do something like this:
for var in request.GET['myvar']:
print(var)
However, when you try that you only get the last value passed in the url i.e in the example above you will get 567, and the result in the shell will be:
5
6
7
However, when you do a print of request.GET it seems like it has a list i.e:
<QueryDict: {u'myvar': [u'123', u'567']}>
Ok Update:
It's designed to return the last value, my use case is i need a list.
from django docs:
QueryDict.getitem(key)
Returns
the value for the given key. If the
key has more than one value,
getitem() returns the last value. Raises
django.utils.datastructures.MultiValueDictKeyError
if the key does not exist. (This is a
subclass of Python's standard
KeyError, so you can stick to catching
KeyError
QueryDict.getlist(key) Returns the
data with the requested key, as a
Python list. Returns an empty list if
the key doesn't exist. It's guaranteed
to return a list of some sort.
Update:
If anyone knows why django dev's have done this please let me know, seems counter-intuitive to show a list and it does not behave like one. Not very pythonic!
You want the getlist() function of the GET object:
request.GET.getlist('myvar')
Another solution is creating a copy of the request object... Normally, you can not iterate through a request.GET or request.POST object, but you can do such operations on the copy:
res_set = request.GET.copy()
for item in res_set['myvar']:
item
...
When creating a query string from a QueryDict object that contains multiple values for the same parameter (such as a set of checkboxes) use the urlencode() method:
For example, I needed to obtain the incoming query request, remove a parameter and return the updated query string to the resulting page.
# Obtain a mutable copy of the original string
original_query = request.GET.copy()
# remove an undesired parameter
if 'page' in original_query:
del original_query['page']
Now if the original query has multiple values for the same parameter like this:
{...'track_id': ['1', '2'],...} you will lose the first element in the query string when using code like:
new_query = urllib.parse.urlencode(original_query)
results in...
...&track_id=2&...
However, one can use the urlencode method of the QueryDict class in order to properly include multiple values:
new_query = original_query.urlencode()
which produces...
...&track_id=1&track_id=2&...
I'm doing a program using Twitter API and MongoDB in 2.7 Python language.
I get a timeline and put it in a dictionary, which I want to store in a MongoDB database. To do this I have next code:
def saveOnBD(self, dic):
client = MongoClient("xxxx", "port")
db = client.DB_Tweets_User_Date
collection = db.tweets
collection.insert_many(dic)
I'm debbuging and dic it's not empty but I get next error:
TypeError: documents must be a non-empty list
How can I fix it?
I trying many options, but i solved that question changing the post method.
Instead of:
collection.insert_many(dic)
I used this:
collection.insert_one(dic)
I supose that, as I try to post only a variable(dic) and "insert_many()" is for many variables that retun me the error. That change solved me the question
you can either put in an entry before running the bulk entry function or use insert()
A list of documents must be passed to insert_many method
E.g.:
collection.insert_many([dic])
I want to know if it's possible to get values from this query string?
'?agencyID=1&agencyID=2&agencyID=3'
Why I have to use a query string like this?
I have a form with 10 check boxes. my user should send me ID of news agencies which he/she is interested in. so to query string contains of multiple values with the same name. total count of news agencies are variable and they are loaded from database.
I'm using Python Tornado to parse query strings.
Reading the tornado docs, this seems to do what you want
RequestHandler.get_arguments(name, strip=True)
Returns a list
of the arguments with the given name.
If the argument is not present, returns an empty list.
The returned values are always unicode.
So like this
ids = self.get_arguments('agencyID')
Note that here i used get_arguments, there is also a get_argument but that gets only a single argument.
You can get the whole query with
query = self.request.query
>>> from urlparse import urlparse, parse_qs
>>> url = '?agencyID=1&agencyID=2&agencyID=3'
>>> parse_qs(urlparse(url).query)
{'agencyID': ['1', '2', '3']}
tornado.web.RequestHandler has two methods to get arguments:
get_argument(id), which will get the last argument with name 'id'.
get_arguments(id), which will get all arguments with name 'id' into a list even if there is only one.
So, maybe get_arguments is what you need.
I am using memcached with pylibmc as binaries in my Django app. Now what I want to get list of key values from cache.
Suppose I have this key value pair data in cache,
{'Key_1':[1,2,3]} {'Key_2':[4,5,6]} {'Key_3':[6,7,8]}
I can get a single record by
cache.get('Key_1')
I want to get all Key_*data
cache.get('Key_*')
Anyone suggest a way? or is it possible?
Thanks!
If you have dictionary than you can do something like this:
import re
dict = { 'Key_1':[1,2,3], 'Key_2':[4,5,6], 'Key_3':[6,7,8] }
r = re.compile(r"Key_\d+") // matching expression
matching_keys = filter(r.match, dict.keys())
This way you can get all matching keys and then simply iterate on those keys.
You could either use the mcdict library and iterate through memcached like a normal dictionary or else you could look at the mcdict source code and apply the same technique in your own code.
I'm developing application using Bottle. How do I get full query string when I get a GET Request.
I dont want to catch using individual parameters like:
param_a = request.GET.get("a","")
as I dont want to fix number of parameters in the URL.
How to get full query string of requested url
You can use the attribute request.query_string to get the whole query string.
Use request.query or request.query.getall(key) if you have more than one value for a single key.
For eg., request.query.a will return you the param_a you wanted. request.query.b will return the parameter for b and so on.
If you only want the query string alone, you can use #halex's answer.