Re: Choosing between User.first_name/last_name & username

2008-06-22 Thread Stuart Grimshaw

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
-~--~~~~--~~--~--~---



Re: Choosing between User.first_name/last_name & username

2008-06-22 Thread Nathaniel Whiteinge

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
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.)::

user.get_full_name() or user.username

.. [1] http://www.diveintopython.org/power_of_introspection/and_or.html
.. [2]

The Python 2.5 ternary operator syntax::

value_when_true if condition else value_when_false

is equivalent to::

cond ? valueIfTrue : valueIfFalse
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Choosing between User.first_name/last_name & username

2008-06-22 Thread Stuart Grimshaw

As part of my sign up process, I don't require that people fill in
their first/last name, and just choose to show the username if no name
is entered.

Unfortunatly, I've come across a bit of code that I can't do this in,
which has got me thinking, I'm doing this all over the show, so there
must be a better way of doing it.

Ideally I'd like to just override the __unicode__ of the User method,
but I've been reading about the kerfuffle involved doing that, so does
anyone have any other bright ideas?

The problem that kicked all this off was that I was trying something
like this:

swap_players_form.fields['side_a'].choices = [(player.player.id,
player.player.first_name == '' ? player.player.username :
player.player.first_name) for player in side_a]

but it was throwing syntax errors on "player.player.first_name == '' ?
player.player.username : player.player.first_name)", if I could get
this method to work for now to let me move on that'd be a start.

Any ideas anyone?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---