Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherited hybrid properties not using ModelAlias #2888

Closed
mikemill opened this issue May 8, 2024 · 0 comments
Closed

Inherited hybrid properties not using ModelAlias #2888

mikemill opened this issue May 8, 2024 · 0 comments

Comments

@mikemill
Copy link

mikemill commented May 8, 2024

peewee 3.17.3

This is similar to the issue from 2019 #1969. We are using a base class to define a couple of fields and related hybrid properties so that we can reuse it across multiple models. We've found that if we alias the model then the hybrid properties (and hybrid methods) don't use the alias and instead use the base class.

Example:

from peewee import Model, IntegerField
from playhouse.hybrid import hybrid_property


class Mixin:
    @hybrid_property
    def length(self):
        return self.end - self.start


class Interval(Model, Mixin):
    start = IntegerField()
    end = IntegerField()


Interval2 = Interval.alias()

query = Interval2.select(Interval2.length)

print(query)

The resulting query is
SELECT ("t1"."end" - "t1"."start") FROM "interval" AS "t2"

I've debugged it down to ModelAlias.__getattr__ and obj = self.model.__dict__[attr]. My understanding is that inherited properties are not in the class' __dict__ so that code will throw the KeyError.

Is there a way to address this?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant