Christian Démolis wrote:
> I m disappointed
>
> I already use session.query everywhere in my code.
>
> Maybe this mechanism of "prefiltered select relation without dl all
> the object (proxy)" will appear in a future version of SQL Alchemy (we
> already can indicate the order_by, why not the select column)...
>
> I can t use the defer query option because i need to obtain my object
> in multiple ways. (entire object, only some attribute...)
>
> What do you think of that (it s just an idea)?
> Declare an object twice (one with all the attribute and one with only
> some attributes).
> In my relation, when i want to access to only some attribute i bind
> the relation to the light object
> It s a brutal method, i dont think if it can work...
>

I cannot speak to how feasible it is to add this feature to a relation,
but if it is added then it would likely be built on top of query options
since there is so much overlap. I think your multiple definition
approach is possible, but it is not something I would want to attempt.

When you use defer as a query option, you are telling SQLAlchemy to not
load that attribute for that query only, so I'm not sure why you are
hesitant to use query options. I'm assuming you need a bit more, so here
is another option (untested):

   1. Make the Verouillage.Utilisateur relation a dynamic relation by
      passing lazy='dynamic' to the relation(). This will make the
      relation return a Query instead of a list.
   2. Add a property to Verouillage to wrap the dynamic relation, e.g.:
      @property
      def Utilisateur_Login(self):
          q = self.Utilisateur
          # Repeat for all Utilisateur attributes you do not want to load
          q = q.options(defer("some_Utilisateur_attribute"))
          return q
   3. Use the new property instead of the original relation when you
      want to restrict your SELECT columns.

I'm not sure how well this technique is supported. You should test with
SQL echo enabled on your engine to confirm that it works.

I do have a side question: if bandwidth is such a concern, maybe it
makes more sense to make ALL mapped attributes deferred by default (via
sqlalchemy.orm.deferred), and selectively undefer attributes on a
per-query basis?

-Conor

> 2009/11/24 Conor <[email protected]
> <mailto:[email protected]>>
>
>     Christian Démolis wrote:
>     > Cool, it's very powerful, it will allow me to save my Bandwidth
>     > because i take just what i want and not the entire object.
>     >
>     > Thanks Conor
>     >
>
>     I think I gave you the wrong impression: association_proxy does not
>     replace the original relation in any way. You are still using a full
>     SELECT when using the association_proxy, because the association_proxy
>     does its thing outside of SQL.
>
>     If you need to pick and choose columns in the SELECT statement, I
>     would
>     advise doing that in the query:
>     q = session.query(Utilisateur.Login)
>     q = q.join(Utilisateur.verouillage)
>     q = q.filter(Verouillage.id == some_id)
>     logins = q.all()
>
>     Alternatively, you can look into using the sqlalchemy.orm.defer query
>     option to tell the query to NOT load a given column when loading a
>     given
>     class. More info at
>     http://www.sqlalchemy.org/docs/05/reference/orm/query.html#query-options.
>
>     -Conor
>
>     > 2009/11/24 Conor <[email protected]
>     <mailto:[email protected]>
>     > <mailto:[email protected]
>     <mailto:[email protected]>>>
>     >
>     >     Christian Démolis wrote:
>     >     > Thx for your answer Thomas
>     >     >
>     >     > I want the attribute to not return the complete object
>     just some of
>     >     > the attribute of the other table.
>     >     >
>     >     > In my case Utilisateur has some attributes : Login, Nom,
>     Prenom, ...
>     >     > I dont want Verouillage.LeNomDuUtilisateur to return
>     complete object
>     >     > Utilisateur
>     >     > I want it to return only Login attributes.
>     >     >
>     >     > Primaryjoin seems to work on condition (where clause) and
>     not on
>     >     > select condition (select ... in a query)
>     >     >
>     >     > Is it possible to limit selected attributes in the relation?
>     >     >
>     >
>     >     You can use sqlalchemy.ext.associationproxy.association_proxy to
>     >     turn a
>     >     collection of related objects into a collection of related
>     object
>     >     attributes:
>     >     _LeUtilisateur = relation(Utilisateur,
>     >     backref=backref('verrouillage'))
>     >     LeNomDuUtilisateur = association_proxy('_LeUtilisateur',
>     'Login')
>     >
>     >     -Conor
>     >
>     >     > 2009/11/24 Tefnet Developers - Tomasz Jezierski
>     >     <[email protected] <mailto:[email protected]>
>     <mailto:[email protected] <mailto:[email protected]>>
>     >     > <mailto:[email protected] <mailto:[email protected]>
>     <mailto:[email protected] <mailto:[email protected]>>>>
>     >     >
>     >     >     Dnia 2009-11-24, Wt o godzinie 11:18 +0100, Christian
>     >     Démolis pisze:
>     >     >     > Is it possible to put a filter on a relation in the
>     >     declaration?
>     >     >     >
>     >     >     > Example :
>     >     >     > LeNomDuUtilisateur = relation(Utilisateur,
>     >     >     > filter_by=Utilisateur.Login,
>     backref=backref('verrouillage'))
>     >     >
>     >     >     I'm not sure what exactly your example means.. but if you
>     >     want extra
>     >     >     filters on relation, you can change primaryjoin
>     >     >
>     >    
>     
> http://www.sqlalchemy.org/docs/05/mappers.html#specifying-alternate-join-conditions-to-relation
>     >     >
>     >     >     something like:
>     >     >     LeNomDuUtilisateur = relation(Utilisateur,
>     >     >     primaryjoin=sqlalchemy.and_(defaultjoincondition,
>     >     >     yourextrafiltercondition), backref='verrouillage')
>     >     >
>     >     >
>     >     >     Tomasz Jezierski
>     >     >     Tefnet
>     >     >     www.tefnet.pl <http://www.tefnet.pl>
>     <http://www.tefnet.pl> <http://www.tefnet.pl>
>     >     >
>     >
>

--

You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to