On Jun 22, 11:48 pm, Nathaniel Whiteinge <[EMAIL PROTECTED]> wrote: > Stuart Grimshaw wrote: > > but it was throwing syntax errors on "player.player.first_name == '' ? > > player.player.username : player.player.first_name)" > > It looks like you're trying to use a ternary operator here, but only > Python 2.5 and later has one (and the syntax is different [2]). The
Thanks Nathaniel, I'm on 2.5, but was just using the wrong syntax ,,, > good news is that you can accomplish what you want without using a > ternary operator since Python boolean operators return values instead > of just true or false [1]. Try the following instead:: > > swap_players_form.fields['side_a'].choices = [( > player.player.id, > player.player.first_name or player.player.username > ) for player in side_a] > > I similar idiom that I make frequent use of if I don't require that a > user enter a first or last name and therefore need to use the username > as a fallback is the following. (Note the built-in Django method > ``get_full_name`` always returns a string, but the first name or the > last name can be empty.):: I was going for the full name, but using first_name as a starting point, I figured if that was blank, they probably both were. Thanks for pointing out get_full_name and for the introspection stuff too, just what I needed. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---