yeah you can use defaultload.load_only

defaultload(ModelA.model_b).load_only(ModelB.only_field)



On Wednesday, January 4, 2023 at 3:15:02 AM UTC-5 [email protected] 
wrote:

> Hi,
>
> This might be a strange question, but I tried to find this in the 
> documentation to no avail.
>
> Is it possible to use something like load_only to override which columns 
> are loaded in when loading a relationship (as in, a relationship that is 
> not loaded at first with the original query)?
>
> Something like:
> class ModelB:
>   ...
>
> class ModelA:
>   model_b = relationship("ModelB")
>
> model_a = 
> session.query(ModelA).options(load_only(Model_b.only_field_i_want_in_the_future)).filter(ModelA.id==1).first()
>
> It's a bit strange, but I want to ensure if someone loads the model_b 
> property in the future, only specific columns are loaded in at first.
>
> I can do this if I just query for model_b via the foreign key instead of 
> using the relationship property, but I'd like to avoid that if possible.
>
> Sorry if this question is a bit weird/confusing, it's kind of a strange 
> use case.
>
> Thanks,
> Tony
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/8a25a58e-73a0-42c1-94ba-6007860e1e3fn%40googlegroups.com.

Reply via email to