Hello,
--- On Wed, 6/30/10, alina <[email protected]> wrote:
> > > is there a way to define a function that would
> return the value of a
> > > file and take the field name as a parameter?
> >
> > > Something like this:
> >
> > > class PersonReqs(Entity):
> > > ...
> > > def get_field_value(self, field_name) :
> > > return self.value(field_name)
> >
> > try:
> > class PersonReqs(Entity):
> > def get_field_value(self, field_name) :
> > return getattr(self,field_name)
> >
> > should work if the Fields are defined properly
I propose to you this code, it works even for nested attributes like for
example get_value_of("a_relation.another_relation.some_field","default value if
not found")
<code>
Class SomeModel(Entity):
def get_values(self,attrs):
return [self.get_value_of(attr) for attr in attrs]
def get_value_of(self,attr,default=""):
"""
"""
path = attr.split(".")
inst = self
for node in path :
inst = getattr(inst,node,None)
if inst == None:
inst = default
break
return inst
</code>
Hope this helps.
--
You received this message because you are subscribed to the Google Groups
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlelixir?hl=en.