This question already has answers here:
Query for list of attribute instead of tuples in SQLAlchemy
(4 answers)
SQL Alchemy ORM returning a single column, how to avoid common post processing
(7 answers)
Closed 5 years ago.
I'm trying to get a list of integers, for this query:
session.query(C.ex_id).filter(c.foo==foo).all()
I get a list of tuples instead of list of integers. I don't want to iterate over the list I get, I want to get it form the query itself.
What can I do?
This may not be exactly what you want, but one work around is:
some_list = map(lambda (x,):x, session.query(C.ex_id).filter(c.foo==foo).all())
Related
This question already has answers here:
use mongoose model.find() to get all entries of only 1 field
(2 answers)
How to select a single field for all documents in a MongoDB collection?
(24 answers)
Closed 4 years ago.
I have a collection in MongoDB named (Emotion), it has 13 documents and one of the attributes of the document is (maxEmotion)
I'm using Python, I want to return all the values of that attribute, so at the end I will have 13 results.
I've tried:
emotions = db.Emotion.distinct("maxEmotion")
But I didn't get the correct results.
This is a picture from the collection:
Thank you.
This question already has answers here:
SQLAlchemy get every row the matches query and loop through them
(2 answers)
Closed 4 years ago.
I need select * from new_entry where batch=2018.
I tried query.filter(NewEntry.batch==batch).first() i got first record in database but i need all columns which is matched to batch.What can I do?
Since you are using first() you will only get the first result, to get all the results as a list you have to use all()
query.filter(NewEntry.batch==batch).all()
This question already has answers here:
How do I do an OR filter in a Django query?
(8 answers)
Closed 4 years ago.
I have a requirement:
in my filter, I want to filter like bellow:
messages = Message.objects.filter(to_user=user or from_user=user).all()
I mean if the to_user==user or from_user==user all be queried out.
I found the bellow related post:
How to use OR filter condition in queryset?
But this is one params, you see this is different between my requirement scenario.
Use Q objects, that allows complex lookups.
from django.db.models import Q
messages = Message.objects.filter(Q(to_user=user) | Q(from_user=user))
This question already has answers here:
Testing a filter on a document on client-side
(2 answers)
Closed 6 years ago.
Is there a way to use the pymongo find method on a list (or iterable) of dictionaries instead on performing the search on the database?
I have a list of dictionaries and I'd like to filter them using queries like in pymongo.
Is there any library that can do that?
you don't need an external library to accomplish that simple task. If i understand correctly you just need to filter a list of dictionaries based on some value.
def find(dict_list, key, value_list):
return [dict for dict in dict_list if dict[key] in value_list]
You can then pass a list of dictionaries a key to match and values of that key that you want to search.
This question already has answers here:
Query for list of attribute instead of tuples in SQLAlchemy
(4 answers)
Closed 5 years ago.
I have simple question regarding SQLAlchemy, is it possible to get the rows from the result as scalars instead of tuples? In other words I want an equivalent to:
[i[0] for i in self.archive.query(IRTerm.term).distinct()]
Thank you
No built in way in SQLAlchemy, but with python it isn't too hard. The example you gave works fine. You can also do map(itemgetter(0), query) or for value, in query:.
Since 0.6.5 you can use query.as_scalar (ref).