Skip to content Skip to sidebar Skip to footer

Sqlalchemy Attributeerror: 'property' Object Has No Attribute 'translate'

Situation Users make purchases, which are stored as transactions in 3 different tables (depending on the type). I need to calculate total amount of transactions/purchases of female

Solution 1:

As the docs Using Descriptors and Hybrids says you should be using hybrid_property to be able to use them in your queries.

Take a look at example in docs:

classEmailAddress(Base):
    __tablename__ = 'email_address'id = Column(Integer, primary_key=True)

    # name the attribute with an underscore,# different from the column name
    _email = Column("email", String)

    # then create an ".email" attribute# to get/set "._email"    @propertydefemail(self):
        return self._email

While our EmailAddress object will shuttle the value through the email descriptor and into the _email mapped attribute, the class level EmailAddress.email attribute does not have the usual expression semantics usable with Query. To provide these, we instead use the hybrid extension

Solution 2:

The best solution here is probably using @hybrid_property but I had problems to make it work.

I came up with a completely different solution, using a classical method. This was super fast and so far I dont see any downsides:

# Normal method to calculate | Best case would probably be @hybrid_methoddefcount_credits_purchases(self, start_date, end_date, gender):
    trans = db_session.query(Transaction_1).filter(Transaction_1.type == "credits", Transaction_1.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    trans_vk = db_session.query(Transaction_2).filter(Transaction_2.type == "credits", Transaction_2.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    trans_stripe = db_session.query(Transaction_3).filter(Transaction_3.type == "credits", Transaction_3.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    value = trans + trans_vk + trans_stripe
    return value

Call in python:

total_purchases_males_credits = Users().count_credits_purchases(start_date, end_date, "1")

I would still like to know how good ths approach is compared to hybrid_property?

EDIT:

Its also possible to use @hybrid_method:

@hybrid_methoddefcount_credits_purchases(self, start_date, end_date, gender):
    trans = db_session.query(Transaction_1).filter(Transaction_1.type == "credits", Transaction_1.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    trans_vk = db_session.query(Transaction_2).filter(Transaction_2.type == "credits", Transaction_2.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    trans_stripe = db_session.query(Transaction_3).filter(Transaction_3.type == "credits", Transaction_3.user_id == Users.id).filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == gender).count()
    value = trans + trans_vk + trans_stripe
    return value

And use it:

total_purchases_males_credits = db_session.query(func.sum(Users.count_credits_purchases(start_date, end_date, "1"))).scalar()

Post a Comment for "Sqlalchemy Attributeerror: 'property' Object Has No Attribute 'translate'"