I am working with flask and redis. I've decided to try the rom redis orm (http://pythonhosted.org/rom/) to manage some mildly complex data structures. I have a list of objects, lets say:
urls = ['www.google.com', 'www.example.com', 'www.python.org']
I also have the rom model:
class Stored_url(rom.Model):
url = rom.String(required=True, unique=True, suffix=True)
salt = rom.String()
hash = rom.String()
created_at = rom.Float(default=time.time)
This appears to be working on my dev setup. I have loaded about 25 'Stored_url' objects into REDIS (confirmed at cmd line). I am trying to come up with a way of getting all the objects of type Stored_url into a python list.
>>> test = Mymodels.Stored_url
>>> type(test)
Out[35]: rom._ModelMetaclass
>>> h =test.query.filter(url ='.').all()
>>> h.count()
Traceback (most recent call last):
File "C:\envs\virtalenvs\flaskenv\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
h.count()
TypeError: count() takes exactly one argument (0 given)
I thought h would have a list of objects. What am I doing wrong? (I was filtering by "." because i figured on urls would have it)
解决方案
There are two problems with the code you have provided which explain why you get the results that you get.
The first problem is that your query test.query.filter(url ='.').all() will return an empty list. This will return an empty list simply because you don't have a valid index to be used with the filter you have specified. You do have 2 indexes for that column - a unique index (useful for looking up urls by exact string) and a suffix index (useful for finding urls that end with a certain string) - but neither offer the ability to filter by what would be in the relational world a 'like' query. A prefix index (created with prefix=True) would let you use test.query.like(url='*.'), but that would be very slow (it does an index scan instead of direct lookup[1]).
To help prevent index/query-related issues like this, I have added QueryError exceptions when users attempt to filter their data by indexes that don't exist. I'll be releasing 0.31.4 a bit later tonight with those changes.
The second error you have, which is the cause of the exception, is that you call .count() without an argument. At the point of your h.count() call, type(h) == list, and Python list objects require an argument to count values equal to the provided argument in the list. If you skipped the .all() portion of your original query, you would get a query object back. That query object has a .count() method, and would return a count of matched results.
[1] Not all 'like' queries in rom are slow, but those that are fast require non-wildcard prefixes to limit data ranges to scan/filter.