SQLAlchemy AttributeError:'属性'对象没有属性'翻译' [英] Sqlalchemy AttributeError: 'property' object has no attribute 'translate'

查看:565
本文介绍了SQLAlchemy AttributeError:'属性'对象没有属性'翻译'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

用户进行购买,并将其作为交易存储在3个不同的表中(取决于类型).我需要计算男女用户的交易/购买总额,所以我需要查看所有3个表.

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 and male users, so I need to look into all 3 tables.

为此,我在User表中创建了@property:

For this I created a @property in the User table:

@property
def count_credits_purchases(self):
    trans = object_session(self).query(Transaction_1).filter(Transaction_1.type == "credits").with_parent(self).count()
    trans_vk = object_session(self).query(Transaction_2).filter(Transaction_2.type == "credits").with_parent(self).count()
    trans_stripe = object_session(self).query(Transaction_3).filter(Transaction_3.type == "credits").with_parent(self).count()
    value = trans + trans_vk + trans_stripe
    return int(value)

我正在尝试使用sqlalchemy func.sum()计算购买总额:

I am trying to calculate the total amount of purchases by using sqlalchemy func.sum():

total_purchases_males_credits = db_session.query(func.sum(Users.count_credits_purchases))
.filter(Users.date_added >= start_date, Users.date_added <= end_date, Users.gender == "1")
.scalar()

问题

AttributeError: 'property' object has no attribute 'translate'

translate方法是字符串方法,这是怎么回事?我肯定在count_credits_purchases中返回了一个整数.

The translate method is a string method, what is happening here? I definitely return an integer in count_credits_purchases.

我进行了测试,并检查每个用户的值始终正确:

I made a test and checking the value per user is always correct:

all_users = db_session.query(Users).limit(200)
for user in all_users:
    print (user.count_credits_purchases) # gives correct result

我可以创建一个变量并在循环中进行计算,但是它效率极低,如果有5万用户,则可能需要1个小时.我需要了解如何使用@property属性

I could make a variable and calculate it in the loop, but it is super unefficient and would need probably 1 hour if there are 50k users. I need to understand how to work with the @property attribute

推荐答案

这里最好的解决方案可能是使用@hybrid_property,但是我无法使其正常工作.

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_method
def count_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

调用python:

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

我仍然想知道与hybrid_property相比,这种方法有多好?

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

也可以使用@hybrid_method:

@hybrid_method
def count_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

并使用它:

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

这篇关于SQLAlchemy AttributeError:'属性'对象没有属性'翻译'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆