Background
使用django框架会在原数据库中生成一些框架所使用的表,其中User、Permission、Group三个model的表名没有django前缀,为了与项目中的业务表区分开,我们这里把django框架使用的表,没有django前缀的给它加上。
1、找到auth模块位置
Python安装环境目录/lib/python3.x/site-packages/django/contrib/auth
- 例如我的如下所示
/usr/local/python3/lib/python3.7/site-packages/django/contrib/auth
2、将auth目录下的migrations目录清空(__init__.py
除外)
缓存不清除,有时执行失败。
3、修改models.py文件(先备份一下)
我们在User、Permission、Group这三个model中加入
db_table
属性,如下所示。
- User
class User(AbstractUser):
"""
Users within the Django authentication system are represented by this
model.
Username and password are required. Other fields are optional.
"""
class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
# 加入这一行,重写默认表名
db_table = "django_auth_user"
- Permission
class Permission(models.Model):
"""
The permissions system provides a way to assign permissions to specific
users and groups of users.
The permission system is used by the Django admin site, but may also be
useful in your own code. The Django admin site uses permissions as follows:
- The "add" permission limits the user's ability to view the "add" form
and add an object.
- The "change" permission limits a user's ability to view the change
list, view the "change" form and change an object.
- The "delete" permission limits the ability to delete an object.
- The "view" permission limits the ability to view an object.
Permissions are set globally per type of object, not per specific object
instance. It is possible to say "Mary may change news stories," but it's
not currently possible to say "Mary may change news stories, but only the
ones she created herself" or "Mary may only change news stories that have a
certain status or publication date."
The permissions listed above are automatically created for each model.
"""
name = models.CharField(_('name'), max_length=255)
content_type = models.ForeignKey(
ContentType,
models.CASCADE,
verbose_name=_('content type'),
)
codename = models.CharField(_('codename'), max_length=100)
objects = PermissionManager()
class Meta:
verbose_name = _('permission')
verbose_name_plural = _('permissions')
# 加入这一行,重写默认表名
db_table = "django_auth_permisssion"
unique_together = [['content_type', 'codename']]
ordering = ['content_type__app_label', 'content_type__model', 'codename']
def __str__(self):
return '%s | %s' % (self.content_type, self.name)
def natural_key(self):
return (self.codename,) + self.content_type.natural_key()
natural_key.dependencies = ['contenttypes.contenttype']
- Group
class Group(models.Model):
"""
Groups are a generic way of categorizing users to apply permissions, or
some other label, to those users. A user can belong to any number of
groups.
A user in a group automatically has all the permissions granted to that
group. For example, if the group 'Site editors' has the permission
can_edit_home_page, any user in that group will have that permission.
Beyond permissions, groups are a convenient way to categorize users to
apply some label, or extended functionality, to them. For example, you
could create a group 'Special users', and you could write code that would
do special things to those users -- such as giving them access to a
members-only portion of your site, or sending them members-only email
messages.
"""
name = models.CharField(_('name'), max_length=150, unique=True)
permissions = models.ManyToManyField(
Permission,
verbose_name=_('permissions'),
blank=True,
)
objects = GroupManager()
class Meta:
verbose_name = _('group')
verbose_name_plural = _('groups')
# 加入这一行,重写默认表名
db_table = "django_auth_group"
def __str__(self):
return self.name
def natural_key(self):
return (self.name,)