0

I have a dictionary of the sort

{'category_id': 'women_shoes', 'product_id': 486778462L}

Here category_id is my table name in MYSQL database. I am trying to get the specified product_id from the table women_shoes. I am able to achieve this through this code

class_name = globals()[each_itemset["category_id"].capitalize()]
table_values = session.query(class_name).filter(class_name.product_id == each_itemset["product_id"])
for each in table_values:               
    print each.brand_name               
    name = "brand_name"

Up til here things work fine and I am able to get the brand_name of the product. What I want is that instead of giving the statement

print each.brand_name

I want to do

name = "brand_name"
print each.name

because I don't want to specify the exact table name myself. I want to get the table column names from class_name.table.columns.keys(), and iterate over it to get each column name and supply it to name one by one.

I get the following error when I do this

Traceback (most recent call last):
  File "main_site.py", line 14, in <module>
    json_data = db_access.get_styles_from_db("work")
  File "C:\Python27\Projects\Clothe Studio Recommendation Project\util\db_access.py", line 149, in get_styles_from_db
    calc_outfit_scores(outfit_dict, session)
  File "C:\Python27\Projects\Clothe Studio Recommendation Project\util\db_access.py", line 192, in calc_outfit_scores
    print each.name
AttributeError: 'Women_shoes' object has no attribute 'name'

I have searched through the SQLAlchemy documentation and SO but don't seem to find an answer. What should be done in this scenario? Your help is appreciated.

4
  • You should review your question before submitting to ensure it's conveying your question clearly. For example, where did you define each_itemset? We can guess each_itemset is the dict you've defined on top but still just take this as a tip to review well and then submit. You will get better responses too! Commented Apr 19, 2016 at 22:08
  • Im not really sure what the goal is, your objected statement is a little odd. You assign a static string to a variable and then try to access a attribute that doesnt exist. but if you are trying to see the object dictionary it could be as simple as each.__dict__ using the builtin python. or assign name = each.brand_name and then just use name instead of each.name. Commented Apr 19, 2016 at 22:12
  • Thanks Amir for your suggestion. I would keep this in mind Commented Apr 19, 2016 at 22:17
  • @Busturdust Thanks for your solution. each.__dict__ did the trick. Whereas name = each.brand_name gave me the same error as before. I had tried that already. each.__dict__ is the way to go. If you can give it an as answer, I can mark it. Thanks once again Commented Apr 19, 2016 at 22:18

2 Answers 2

1

If you are simply trying to generate a dictionary of the objects attributes: values, you can use the built in python object attribute dictionay __dict__

in your case

for each in table_values:               
    print each.__dict__

If there are any Foreign Keys or cast like mechanisms youd like to follow / use, you will need implement a custom to_dict() method for the class, and use that

Sign up to request clarification or add additional context in comments.

Comments

0

You could use getattr to retrieve the attribute in a more generic way.

for each in table_values:      
    name = 'brand_name'
    value = getattr(each, name)
    print(value)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.