问题描述
愚蠢的问题时间在Django中,如果要指定模型表的名称,可以这样做: class MyModel(models。模型):
...
class Meta:
db_table ='mymodel'
可以以类似的方式更改Django的默认auth_user表的名称吗?我总是对用户表执行手动查询,输入'user'比'auth_user'要快得多。我查看了Django文档,并进行了一些互联网搜索,我没有看到任何人解决这个问题。
谢谢!
和给了你很好的答案。您可以扩展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表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!