SQLAlchemy AttributeError: 'Attribute' object has no attribute 'translation'


Roman

Condition

The user makes a purchase and it is stored as a transaction in 3 different tables (depending on the type). I need to calculate the total transactions/purchases of male and female users, so I need to see all 3 tables.

For this, I created one @propertyin 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)

I am trying to calculate the total amount of purchases 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()

question

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

The pan method is a string method, what's going on here? I definitely return an integer count_credits_purchasesin .

I tested and checked that the value is always correct for each user:

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

I can create a variable and do the calculation in a loop, but it's horribly inefficient and could take 1 hour if there are 50k users. I need to know how to use the @propertyproperty

Roman

The best solution is probably to use @hybrid_property.

I came up with a completely different solution using the classic approach. This is very fast and I don't see any downsides so far:

# 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

Call python:

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

I'm still wondering how good is this approach compared to hybrid_property?

edit:

It is also possible to use @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

and use it:

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

Related


SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

WiGeeky: I have two tables named usersand permissionsI want to create a relationship between them using the specified table userPermissions. Here's what my code looks like: 类User(Base): __tablename__ ='用户' id =列(Integer,primary_key = True) first_

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError: 'SQLAlchemy' object has no attribute 'FieldType'

OK Python, flask and sqlalchemy newbies here. I am writing a small application to collect data (from an excel file) from different groups (departments). Each department will have associated tables and associated fields. Fields will have predefined types (numbe

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

AttributeError: 'SQLAlchemy' object has no attribute 'datetime'

Wayne I'm following a youtube tutorial on using Flask and when I run this code with python -m flask runit it shows this AttributeError: 'SQLAlchemy' object has no attribute 'datetime'. How can I fix this? from flask import Flask, render_template, url_for from

SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

WiGeeky: I have two tables named usersand permissionsI want to create a relationship between them using the specified table userPermissions. Here's what my code looks like: 类User(Base): __tablename__ ='用户' id =列(Integer,primary_key = True) first_

AttributeError: 'SQLAlchemy' object has no attribute 'FieldType'

OK Python, flask and sqlalchemy newbies here. I am writing a small application to collect data (from an excel file) from different groups (departments). Each department will have associated tables and associated fields. Fields will have predefined types (numbe

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

SQLAlchemy: AttributeError: 'table' object has no attribute 'id'

Yevgeny I am building a database that matches my local product catalog with foreign product catalogs. 'Catalog'SQLAlchemy works fine when I have only unique tables . After adding the tables Competitors, Competitors_catalogall attempts to add records to the two

AttributeError: '' object has no attribute''

gentlemen. Dutch I have a question, I am using python 3 to code. The code is about getting the news of the website onto my canvas. But I keep getting this error which says: AttributeError: 'NewsFeed' object has no attribute 'canvas'. Here is my code: from tkin

AttributeError: object has no attribute

AKHIL MATHEW In my custom Odoo module I have to call a python function when a button is clicked. I followed the exact method from the documentation. But I get error as below.AttributeError: 'book.meeting' object has no attribute 'checkout_function' My code is