On Sat, Jan 17, 2015 at 12:58 PM, Micky Hulse <mickyhu...@gmail.com> wrote:
> Great, that's what I wanted to know. Thank you for pushing me in the
> right direction and for the sample code.

Thanks again Vijay! After doing a bit of research, I slightly modified
your code (I needed an easy way to check nested attributes):

from operator import attrgetter
def get_fk_field(obj, path):
    '''
    Returns (nested) attribute if it exists.
    Examples:
    get_fk(self, 'vehicle.manufacturer.title')
    get_fk(self, 'vehicle.model')
    '''
    try:
        attr = attrgetter(path)
        return attr(obj)
    except AttributeError:
        return None

>From what I know, operator.attrgetter works in Python 2.7 and above.

Anyone, pease let me know if the above code could be optimized.

Thanks again!
M

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKzdcNn%3D8VobxFyh-wVzt5_Wjf%3DGTip0YOzy96SdBSNrPXv-aQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to