Skip to content

Filter condition on nested attribute #1183

Open
@ck-on-github

Description

@ck-on-github

Hello,

In my pynamodb model, I have an attribute, which can be an instance of one of the sub-classes of a polymorphic map attribute like:

class ParentClass(MapAttribute):
    cls = DiscriminatorAttribute()

class ChildClass1(ParentClass, discriminator = cls.__name__):
    filter_attr = UnicodeAttribute()

class ChildClass2(ParentClass, discriminator = cls.__name__):
    other_attr = UnicodeAttribute()

class MyModel(Model):
   ...
   attr = ParentClass() 

If I define a filter condition on MyModel.attr.filter_attr like MyModel.attr.filter_attr == value, I get the run time error

    def __getattr__(self, attr: str) -> _VT:
        # This should only be called for "raw" (i.e. non-subclassed) MapAttribute instances.
        # MapAttribute subclasses should access attributes via the Attribute descriptors.
        if self.is_raw() and self._is_attribute_container():
            try:
                return self.attribute_values[attr]
            except KeyError:
                pass
>       raise AttributeError("'{}' has no attribute '{}'".format(self.__class__.__name__, attr))
E       AttributeError: 'ParentClass' has no attribute 'filter_attr'

which makes sense because attr is of type ParentClass, which only has cls attribute.

My question is, is there a way to define a filter on a nested attribute in a pynamodb model, where the nested attribute is an instance of a child class of a polymorphic MapAttribute parent class?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions