Django,当您第一次迁移时,在_uth文件夹下的models.py如何创建初始表? [英] Django, How does models.py under auth folder create initial tables when you migrate very first time?

查看:208
本文介绍了Django,当您第一次迁移时,在_uth文件夹下的models.py如何创建初始表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在Django中创建新项目后第一次迁移,您可以发现Django创建如下所示的表。

  auth_group 
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session

现在我知道这些表是创建的,因为在settings.py中INSTALLED_APPS下的行。

  INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django。 contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

所以我开始在auth文件夹(在我安装的Django文件夹)中查看models.py。我预计在models.py中会有六个类。因为我了解到,由于ORM(Ojbejct关系映射),Class转为表。

  auth_group 
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions

我可以找到类命名为权限,名为Group的类和名为User的类名称在models.py的auth文件夹下。我认为这些表是'auth_permission','auth_group'和'auth_user'。那么其他的呢?(auth_group_permissions,auth_user_groups,auth_user_user_permissions)我想了解这些表是由Django创建的(models.py在auth文件夹中)。我应该在models.py中查看哪些?



我希望能够了解如何创建其他表(django_admin_log,django_content_type,django_migrations,django_session)如果我可以理解如果您还可以解释Django如何创建名为django_migrations和django_session的表格,我将会感激。



提前谢谢。祝你今天愉快。
输入以pgAdmin3显示的图表

解决方案

这可以很容易地解释。

  auth_user_groups 
auth_group_permissions
auth_user_user_permissions

是关系表。哪些用于存储关于模型表如何相关的信息。



例如 auth_user_groups 正在存储用户和组有关系。
如果您执行

  SELECT * FROM auth_user_groups; 
| id | user_id | group_id |
....

这里我们可以看到哪些组与哪些用户有关系



所以基本上django会自动创建这样的表,当你在你的模型上使用 ManyToManyField

回覆评论
当您调用 ./ manage.py migrate 第一次此表存储应用迁移的历史记录。迁移模型可以在 django / db / migrations / recorder.py 中找到。运行新的迁移时,将使用此模型来查看哪些应用程序以及应用于此命令运行的应用程序。这个模型是核心django功能的一部分,这就是为什么你不需要将它添加到 INSTALLED_APPS 中,因为 INSTALLED_APPS 只包含可插拔的应用程序。如果需要,您可以从项目中包含/排除。


If you migrate very first time after making new project in Django, you can find that Django creates tables like below.

auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session

Now I learned that those tables are created because lines under INSTALLED_APPS in settings.py.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

so I started to look into models.py under auth folder(in Django folder where I installed). I expected that there would be six classes in models.py. Because I learned that Class turn to table thanks to ORM(Ojbejct Relation Mapping).

auth_group
auth_group_permissions 
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions

I could find class named 'Permission', a class named 'Group' and a class named 'User' in models.py under auth folder. I think those made tables 'auth_permission', 'auth_group' and 'auth_user'. Then what about other?(auth_group_permissions, auth_user_groups, auth_user_user_permissions) I would like to understand how those tables are created by Django(models.py in auth folder). Where should like look into in models.py to understand that?

I expect that I can understand how other tables are created(django_admin_log, django_content_type, django_migrations, django_session) if I can understand that. I will appreciate if you can also explain how Django creates tables named 'django_migrations' and 'django_session' too.

Thank you in advance. Have a nice day. enter image tables showing in pgAdmin3

解决方案

This can be easily explained.

auth_user_groups
auth_group_permissions
auth_user_user_permissions

Are relational tables. Which are used to store info about how model tables are related.

For example auth_user_groups is storing how users and groups are related. If you do

SELECT * FROM auth_user_groups;
|id|user_id|group_id|
....

And here we can see that which groups are related to which users.

So basically django will automatically create such tables for when you use ManyToManyField on your models

Answering comment Django migration table is created automatically when you call ./manage.py migrate for the first time. This table stores history of applying your migrations. Migration model can be found in django/db/migrations/recorder.py. This model is used when running new migrations to see which are applied and which should be applied on this command run. And this model is part of core django's functionality, that's why you don't need to add it in INSTALLED_APPS because INSTALLED_APPS contain only pluggable apps. Which you can include/exclude from your project if you want.

这篇关于Django,当您第一次迁移时,在_uth文件夹下的models.py如何创建初始表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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