社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

[精华] 看django源代码遇到的几个问题不是很明白

liaozd • 9 年前 • 6966 次点击  
    class AbstractUser(AbstractBaseUser, PermissionsMixin):
    """
    An abstract base class implementing a fully featured User model with
    admin-compliant permissions.

    Username, password and email are required. Other fields are optional.
    """


    ##### 这里的_('username')等,为什么前面要加下划线呢? #####
    username = models.CharField(_('username'), max_length=30, unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, digits and '
                    '@/./+/-/_ only.'),
        validators=[
            validators.RegexValidator(r'^[\w.@+-]+, _('Enter a valid username.'), 'invalid')
        ])
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), blank=True)

    class Meta:            ##### 这个Meta在这里是起什么作用,我记得python类好像没有这么定义的 #####
        verbose_name = _('user')
        verbose_name_plural = _('users')
        abstract = True

    def get_full_name(self):
        """
        Returns the first_name plus the last_name, with a space in between.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

class User(AbstractUser):
    """
    Users within the Django authentication system are represented by this
    model.

    Username, password and email are required. Other fields are optional.
    """
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL' ##### 这里是要做什么?不是很懂了 #####
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/766
 
6966 次点击  
文章 [ 8 ]  |  最新文章 9 年前
Py站长
Reply   •   1 楼
Py站长    9 年前

django 能过 获取用户定义的 AUTH_USER_MODEL 来作为 真正的USER

Py站长
Reply   •   2 楼
Py站长    9 年前

AUTH_USER_MODEL 是为了帮忙用户定义一个用户类 https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#auth-custom-user

Py站长
Reply   •   3 楼
Py站长    9 年前

meta是内部类

Java可以在类的内部定义类,Python同样存在这种语法。例如,汽车由门,车轮等部件组成,可以设计出汽车、门、车轮等3个类。而门,车轮是汽车的一部分,因此把门、车轮表示的类放到汽车的内部。这种在某个类内部定义的类称之为内部类。

liaozd
Reply   •   4 楼
liaozd    9 年前

@toozoofoo 是这样的,谢谢,我看到了,是个本地翻译用的模块 from django.utils.translation import ugettext_lazy as _

另外:源代码位置在django.contrib.auth.models

还差两个疑问:

1,为什么可以class Meta: 这种方式来重构类 2,swappable = 'AUTH_USER_MODEL'这个怎么理解

toozoofoo
Reply   •   5 楼
toozoofoo    9 年前

from django.utils.translation import ugettext as _

lcqtdwj
Reply   •   6 楼
lcqtdwj    9 年前

_是一个合法的变量名,所以肯定被django定义某个函数了,class Meta是在model里设置表相关的一些东西,比如order。我是初学者的理解

olivetree
Reply   •   7 楼
olivetree    9 年前

我觉得,DJango可能是把 _ 写成了一个可调用的对象,Django里面应该有定义 _ 的代码,因为这并不属于Python 的语法。

olivetree
Reply   •   8 楼
olivetree    9 年前

s = _

这个竟然可以运行...我凌乱了,有没有大神解释一下