On 02/28/2017 09:15 AM, Edu Ferreira wrote:
Hello, i’ve been doing a work which i need to use a custom collection
with lazy=’dynamic’, and i saw that it’s not possible, and the right way
to do this is inheritance “Query”, something like this:


from sqlalchemy.orm import Query


|
classMyCollection(Query):…
|


and after:

|



Collector(object):…

    mycollection =relationship(‘Whatever’,lazy=’dynamic’,query_class =
  MyCollection)
|



but, which method do i need to override to get a custom access? like the
next methods that i overrid when i'm using collection_class.


|
defappend(self,i):…


def__getitem__(self,i):…


def__iter__(self):...
|



i read the sqlalchemy code, so do i need to inheritance AppenderQuery
too? I tried to do this in many ways, but i didn’t have success, what is
the good way to do this?



you don't need to override AppenderQuery; when you give it query_class, it will generate a new class internally that adds the AppenderQuery behavior to it.


The dynamic loader is going to be kind of limited on the "write" side, e.g. if you need special behavior when you append to it or such, that's not really what it's intended for, it has the very simple append() / remove() methods but right now there aren't good hooks to change what these do, nor are there good hooks to add new kinds of "writer" methods. If you need custom "write" behavior you should stick with a loader other than "dynamic". Basic events when an "append" / "remove" occurs can be achieved with an attribute event, if your use case is just validation / processing of incoming values.

On the "read" side, the __iter__ that you put on your custom Query class is the one that is used to iterate through items, assuming the object is associated with a Session, so whatever you make __iter__ do, that's the behavior you'll see. Similarly, using a "dynamic" loader one expects to call the ".all()" method to get a fixed collection; you'd want to override that as well to populate the list from Query.all() into your custom collection.





--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to