Peeon peewee - select from multiple tables

I am trying to select two fields from separate tables using peewee. I believe my problem is repeating the resulting object.

I have the following code in Python:

sHeader_Value = (System_Headers .select(System_Headers.SystemHeader_Name, System_Data.System_Value) .join(System_Header_Link) .join(System_Data_Link) .join(System_Data)) 

This code generates the following SQL:

 SELECT t1.`SystemHeader_Name`, t4.`System_Value` FROM `system_headers` AS t1 INNER JOIN `system_header_link` AS t2 ON (t1.`SystemHeader_ID` = t2.`SystemHeader_ID`) INNER JOIN `system_data_link` AS t3 ON (t2.`SystemHeaderLink_ID` = t3.`SystemHeaderLink_ID`) INNER JOIN `system_data` AS t4 ON (t3.`SystemData_ID` = t4.`SystemData_ID`) 

Performing this in the MySQL Workbench, I get a table with two fields: SystemHeader_Name, System_Value .

I am trying to figure out how to get System_Value from a query shell. If I do the following:

 for s in sHeader_Value: print s.SystemHeader_Name, s.System_Value 

I am presented with an AttributeError , indicating that the 'System_Headers' object has no attribute 'System_Value' .

Please note that if I only try to print s.SystemHeader_Name , it runs flawlessly.

How to get values ​​for my System_Value field?

+7
python sql mysql orm peewee
source share
1 answer

I just realized what the problem is. The data object that it returns is the System_Headers object, which is the model for this particular table. Thus, this model does not have the System_Value attribute. Adding naive() to my peewee code, he passed the attribute to my System_Headers model, so I got access to the System_Value field.

Below is the working code:

 sHeader_Value = (System_Headers .select(System_Headers.SystemHeader_Name, System_Data.System_Value) .join(System_Header_Link) .join(System_Data_Link) .join(System_Data) .naive()) 
+10
source share

All Articles