On 01/03/2017 03:10 AM, Daniel Kraus wrote:
Hi,
how can I load a sqlalchemy orm model from a dictionary?
Let's say I have a `User` model with attributes `id`, `name`, `email`
and a relationship `languages`.
I have a dict with `id`, `name`, `email` from users in my cache,
but not `languages` that's rarely used.
Is it possible to create a `User` model from a `user_dict` that behaves
like I would have queried it with dbsession.query(User).get(42)?
I will interpret this literally. This would be:
d = {"id": 1, "name": "x", "email": "y"}
u1 = User(**d)
then you'd persist u1 with session.add() / commit() and you're done.
What I mean in particular is that I want that an access to
`user.languages` creates a subquery and populates the attribute.
OK, so I can't imagine any connection between "I want a model from a
dict" and "I want user.languages to create a subquery", so again lets
take this one phrase literally.
"user.languages" would imply that "user" is an instance of User that's
already in memory. So "user.languages", being a relationship, will emit
a lazy load to the database to load the list of languages. It's not
exactly a subquery, it's a single SELECT statement.
I now about the dogpile caching example which is similar but looks
way too much for what I want.
right, so, this is yet another phrase that appears to have no connection
to the previous two phrases that also appear to have no connection :).
So yes, your SO question is unlikely to get any answers because it is
not apparent what you're looking for.
Here's a way that might make it clear - show some pseudocode. That is:
# "I have a dict"
d = {"id": 1, "name": "x", "email": "y"}
# I want to create a user model, something something about attributes,
subqueries
obj = <something something that shows what you want to do>
session.add(obj)
query = session.query(<something>).filter(<something something>)
assert result == <something something that shows what you want>
These are just some examples of things you can try filling in.
PS, I asked the same question on stackoverflow:
http://stackoverflow.com/questions/41158307/load-sqlalchemy-orm-model-from-dict
Maybe I didn't make clear what I want.
Please tell me if I should rephrase my question.
Thanks in advance,
Daniel
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.