Thanks for the info, took me a while to figure out what to do next,
I haven't delved into django internals much before. I ended up
with....
from django.db.models import ForeignKey
from django.forms import ModelChoiceField
class UserModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
if obj.first_name and obj.last_name:
return "%s.%s" % (obj.first_name[0], obj.last_name)
else:
# return username if first_name or last_name is empty
return obj.username
class UserForeignKey(ForeignKey):
def formfield(self, **kwargs):
defaults = {'form_class': UserModelChoiceField}
defaults.update(kwargs)
return super(UserForeignKey, self).formfield(**defaults)
class TestModel(models.Model):
name = UserForeignKey(User)
# and that's it!, hardly any extra code in the end.
# django :)
thx for the help Sam, i'd of never found that in the docs otherwise.
or it would of taken a really long time :)
On May 17, 3:15 am, Sam Chuparkoff <[email protected]> wrote:
> On Fri, 2009-05-15 at 15:12 -0700, nih wrote:
> > in the admin change form the select box only shows name.username (eg
> > nih)
> > is it possible to show the full name in the select box
>
> You are looking to override label_from_instance(). See here
>
> http://docs.djangoproject.com/en/1.0/ref/forms/fields/#modelchoicefield
>
> sdc
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---