Skip to content
Advertisement

Get value of related object by list of fields in @property

I have two model like this with following fields:

JavaScript

I Also I have this list of fields name:

JavaScript

And finally I want to I create this @property:

JavaScript

But for related objects this isn’t the correct way and I got this error:

‘Profile’ object has no attribute ‘address.postal_code’

How Can I use this list for get value of field of Profile objects and related object values?

Notice:

I could solve this problem by using eval function, but because of this and this post, I preferred to throw this solution away.

Advertisement

Answer

Rather than having Profile nose around in its peers’ internals, I would give each class involved an is_complete property (or method) which determines if the instance is complete, and have Profile.is_complete_profile access those properties to determine if the profile is totally complete.

Something like this (untested)

JavaScript

This way Profile doesn’t need to change if the names of Address‘s fields change, and only minimal changes are required if additional objects get added to the notion of “completeness”.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement