重命名Django的auth_user表? [英] Rename Django's auth_user table?

查看:331
本文介绍了重命名Django的auth_user表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

愚蠢的问题时间在Django中,如果要指定模型表的名称,可以这样做:

  class MyModel(models。模型):
...
class Meta:
db_table ='mymodel'

可以以类似的方式更改Django的默认auth_user表的名称吗?我总是对用户表执行手动查询,输入'user'比'auth_user'要快得多。我查看了Django文档,并进行了一些互联网搜索,我没有看到任何人解决这个问题。



谢谢!

解决方案

Selcuk 丹尼尔·罗斯曼给了你很好的答案。您可以扩展User或AbstractUser类中的任何一个,其差别在于,您还需要在Meta类中添加可交换属性。



这是一个例子:

  from django.contrib.auth.models import AbstractUser#(或简单的用户)

class MyUser(AbstractUser):

class Meta:
swappable ='AUTH_USER_MODEL'#仅适用于AbstractUser
db_table ='user'

但是,请注意使用user作为表名,因为有数据库是一个保留字!!!



祝你好运。


Stupid question time. In Django, if you want to specify the name of a model's table, you'll do this:

class MyModel(models.Model):
    ...
    class Meta:
        db_table = 'mymodel'

Is it possible to change the name of Django's default auth_user table in a similar fashion? I'm always running manual queries against the user table and it would be much faster to type 'user' than 'auth_user'. I've looked at the Django docs and done some Internet searches and I haven't seen anyone address this question.

Thanks!

解决方案

Both Selcuk and Daniel Roseman have given you good answers. You can extend either one of the classes User or AbstractUser, with the small difference that for the latter you will also need to add the swappable attribute in you Meta class.

Here is an example:

from django.contrib.auth.models import AbstractUser # (or simply User)

class MyUser(AbstractUser):

    class Meta:
        swappable = 'AUTH_USER_MODEL'  # ONLY for AbstractUser
        db_table = 'user'

However, pay attention to using 'user' as a table name, because there are databases where this is a reserved word !!!

Good luck.

这篇关于重命名Django的auth_user表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆