damon
damon

Reputation: 8477

django CharField definition

In some django models I have often seen

myfield = models.CharField(_('myfield'))
class_name = models.CharField(_('Type'), max_length=128)

What exactly is the _ and tuple for? I never saw any such in the official django tutorial snippets

Upvotes: 2

Views: 238

Answers (3)

jgabrielsk8
jgabrielsk8

Reputation: 441

from django.utils.translation import ugettext as _

Yes this library is used to translate all strings on your django project, of course you have to mark those translation strings, take a look at this Django Docs

Upvotes: 1

arie
arie

Reputation: 18972

Then you didn't look at the right spots of the documentation:

Specify a translation string by using the function ugettext(). It’s convention to import this as a shorter alias, _, to save typing.

from django.utils.translation import ugettext as _

def my_view(request):
    output = _("Welcome to my site.")
    return HttpResponse(output)

Upvotes: 3

mutantacule
mutantacule

Reputation: 7063

Go see at the top of the file, but it's most often this renamed import:

from django.utils.translation import ugettext as _

(and so it's a function call, not a tuple)

Upvotes: 5

Related Questions