You can definitely do it that way if all you need to do is check if values exist and provide a default if they don't. I do this all the time for my __str__() methods as well.
You also mentioned similar situations that may require more logic checks (variables depending on each other or printing computed values), then the try/except strategy is likely more flexible. -James On Jan 17, 2015 7:16 PM, "Micky Hulse" <mickyhu...@gmail.com> wrote: > On Sat, Jan 17, 2015 at 6:15 PM, Tim Chase > <django.us...@tim.thechases.com> wrote: > > I'm pretty sure that getattr() takes an optional 3rd field for the > > default, so you could use > > Great point, that's very useful. Thanks for clarifying! > > It looks like attrgetter doesn't have similar functionality, but > there's this (found after a quick Google search): > > <https://github.com/bradjasper/attrgettersetter> > > Thanks for example code Tim! > > Cheers, > 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/CAKzdcN%3Dr9gA3diGW0t8tu0z03CoUp%2BhZOm3rfaog35X5rxX5MA%40mail.gmail.com > . > For more options, visit https://groups.google.com/d/optout. > -- 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/CA%2Be%2BciVsd8_ARKf_tv90gVPRKWqHU2kQDWsV46G4ygRb%3DLmRVA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.