Wichert Akkerman | 20 Jun 2012 19:13
Gravatar

mixing association proxies and mixin classes

I am struggling a little bit with mixin classes. The pattern I am trying 
to implement is a mixin-class that adds a list of validated search 
queries to a model. A minimised version of the code is below. The 
problem I am running into is that putting an association_proxy on a 
mixin class does not appear to work here: it always picks the first seen 
class type to create new values instead of picking up what the 
relationship of the current instance requires. With the example below 
that results in this error:

AssertionError: Attribute '_filters' on class '<class '__main__.TypeB'>' doesn't handle objects of
type'<class '__main__.FilterA'>'

My initial though was that this might be fixed by making the 
association_proxy instance itself a declared_attr, but that results in 
other problems.

Is there an alternative way to implement this, or is this a bug in the 
declarative logic?

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr

metadata = MetaData()
BaseObject = declarative_base(metadata=metadata)

class BaseFilter(BaseObject):
     __abstract__ = True
(Continue reading)

Michael Bayer | 21 Jun 2012 00:46
Gravatar

Re: mixing association proxies and mixin classes

do it like this for now:

class FilterMixin(object):
     <at> declared_attr
    def _filters(cls):
        cls.filters = association_proxy('_filters', 'filter')
        return relationship(cls.filter_class,
               cascade='all,delete,delete-orphan')

there's a patch for 0.8 only in http://www.sqlalchemy.org/trac/ticket/2517 .

On Jun 20, 2012, at 1:13 PM, Wichert Akkerman wrote:

> I am struggling a little bit with mixin classes. The pattern I am trying to implement is a mixin-class that
adds a list of validated search queries to a model. A minimised version of the code is below. The problem I am
running into is that putting an association_proxy on a mixin class does not appear to work here: it always
picks the first seen class type to create new values instead of picking up what the relationship of the
current instance requires. With the example below that results in this error:
> 
> AssertionError: Attribute '_filters' on class '<class '__main__.TypeB'>' doesn't handle objects of
type'<class '__main__.FilterA'>'
> 
> 
> 
> My initial though was that this might be fixed by making the association_proxy instance itself a
declared_attr, but that results in other problems.
> 
> Is there an alternative way to implement this, or is this a bug in the declarative logic?
> 
> 
(Continue reading)

Wichert Akkerman | 21 Jun 2012 09:18
Gravatar

Re: mixing association proxies and mixin classes

That works like a charm, thanks!

On 06/21/2012 12:46 AM, Michael Bayer wrote:
> do it like this for now:
>
> class FilterMixin(object):
>       <at> declared_attr
>      def _filters(cls):
>          cls.filters = association_proxy('_filters', 'filter')
>          return relationship(cls.filter_class,
>                 cascade='all,delete,delete-orphan')
>
>
> there's a patch for 0.8 only in http://www.sqlalchemy.org/trac/ticket/2517 .
>
>
>
> On Jun 20, 2012, at 1:13 PM, Wichert Akkerman wrote:
>
>> I am struggling a little bit with mixin classes. The pattern I am trying to implement is a mixin-class that
adds a list of validated search queries to a model. A minimised version of the code is below. The problem I am
running into is that putting an association_proxy on a mixin class does not appear to work here: it always
picks the first seen class type to create new values instead of picking up what the relationship of the
current instance requires. With the example below that results in this error:
>>
>> AssertionError: Attribute '_filters' on class '<class '__main__.TypeB'>' doesn't handle objects of
type'<class '__main__.FilterA'>'
>>
>>
>>
(Continue reading)


Gmane