简体   繁体   中英

Django Models - Field Syntax

Quick question regarding some syntax when creating django models. If you take a look at the example models.py file below, you'll see that each of the four fields contain a repeat of the field name as a string in parentheses preceded my an underscore. I assume this is some kind of visual representation for when this is encountered in a form, but that seems to happen automatically in admin with out the _('field name').

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)

All of the built-in field types inherit from the Field class . If you look at the __init__() call for that class, you'll see that the first parameter in the function is the verbose_name parameter. So that tells us what these strings map to.

The _('some text') construct is a fairly prevalent syntax for doing internationalization (also known as i18n ). I've seen this syntax in other languages like PHP. This allows the verbose name to be rendered in various languages when the code is translated. As you note in the comments above, the function ugettext_lazy is imported as the alias _ . This allows programmers (all of whom are lazy), to type _('some text') instead of ugettext_lazy('some text') in every location where text needs to be translated. Since there could be hundreds (or thousands) of hard coded strings in an application, it saves the programmer lots of time in the long run.

It's an ugly hack, but it gets the job done. I dislike it particularly in Python since it overrides the sometimes useful _ name, which often gets used as a placeholder variable name when you want to throw a value away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM