i18n and choices from database

93 views
Skip to first unread message
Message has been deleted

sircco

unread,
Nov 3, 2007, 2:24:37 PM11/3/07
to Django users
lets say i have a model

from django.utils.translation import ugettext_lazy as _

class Person (models.Model):

GENDER = (
(0, _('Male'),
(1,_('Female')
)

name = models.CharField(maxlength=100)
gender = models.IntegerField(choices=GENDER)


and i show it on the form with localized output ....
after submit i get values ( 0,"name" ) in the database.
Question is how to show query from database on template with localized
output

TIA,
Damir

RajeshD

unread,
Nov 3, 2007, 2:38:56 PM11/3/07
to Django users
> Question is how to show query from database on template with localized
> output

Try this in your template (assuming p is a person model instance
available to your template):

{{ p.get_gender_display }}

See: http://www.djangoproject.com/documentation/db-api/#get-foo-display

Also, another suggestion: consider using more intuitive keys for your
gender field instead of 0, 1. You could define it as a CharField and
change choices keys to M, F instead of 0, 1 (or even better "male",
"female"). If you're worried about performance, you can even set
db_index=True on the gender field.

sircco

unread,
Nov 3, 2007, 3:06:44 PM11/3/07
to Django users
tnx, works even with internationalized data!

Damir

Reply all
Reply to author
Forward
0 new messages