简体   繁体   English

使用django.contrib.auth.models导入用户时,是否需要在models.py中创建单独的类?

[英]Do I need to create a separate class in my models.py when using the django.contrib.auth.models import user?

The import statement import the needed parts. import语句导入所需的零件。 but is the "user" class already made when you put that into your installed apps? 但是,当您将其放入已安装的应用程序中时,是否已经制作了“用户”类? or do you still need to clarify in models.py in order to make the table in the db? 还是您仍然需要在models.py中进行说明才能在db中制作表格? or can someone expand on how to use django users and sessions? 或者有人可以扩展有关如何使用django用户和会话的信息? I'm looking over the django docs right now and they all just go over how to use the thing once. 我现在正在查看django文档,而它们都只是简单介绍了如何使用它。 they never put the code in a syntax where users are going to be the ones using the code through a browser and not you through a python shell. 他们永远不会将代码放在语法中,用户将成为通过浏览器而不是通过python shell使用代码的用户。

All installed apps can contribute to the database schema. 所有已安装的应用程序都可以参与数据库架构。 django.contrib.auth.models contributes, among others, the auth_user table behind the django.contrib.auth.models.User model, therefore you do not have to worry about recreating it unless you have a specific reason to do so. django.contrib.auth.models贡献了django.contrib.auth.models.User模型后面的auth_user表,因此,除非有特殊原因,否则您不必担心重新创建它。

There's a number of things going on here. 这里发生了很多事情。 As you're aware, Django comes with a number of "contrib" packages that can be used in your app. 如您所知,Django附带了许多可在您的应用程序中使用的“ contrib”软件包。 You "activate" these by putting them into your INSTALLED_APPS . 您可以通过将它们放入INSTALLED_APPS “激活”它们。

When you run python manage.py syncdb , Django parse the models.py files of every app in INSTALLED_APPS and creates the associated tables in your database. 当您运行python manage.py syncdb ,Django会分析INSTALLED_APPS中每个应用程序的models.py文件,并在数据库中创建关联的表。 So, once you have added django.contrib.auth to your INSTALLED_APPS and ran syncdb , the tables for User and Group are there and ready to be used. 因此,将django.contrib.auth添加到INSTALLED_APPS并运行syncdbUserGroup的表就可以使用了。

Now, if you want to use these models in your other apps, you can import them, as you mention, with something like from django.contrib.auth.models import User . 现在,如果您想在其他应用程序中使用这些模型,则可以使用from django.contrib.auth.models import User类的方法将其导入(如您所述)。 You can then do something like create a ForeignKey , OneToOneField or ManyToManyField on one of your models to the User model. 然后,您可以对User模型中的一个模型执行类似创建ForeignKeyOneToOneFieldManyToManyFieldUser When you do this, no tables are created (with the exception of ManyToManyField ; more on that in a bit). 当您执行此操作时,不会创建任何表( ManyToManyField除外; ManyToManyField更多)。 The same table is always used for User , just as for any of your own models that you might create relationships between. 始终将同一表用于User ,就像您可能在其之间创建关系的任何自己的模型一样。

ManyToManyField s are slightly different in that an intermediary table is created (often called a "join table") that links both sides of the relationship together. ManyToManyField的不同之处在于创建了将关系的两侧链接在一起的中间表(通常称为“联接表”)。 However, this is purely for the purposes of that one particular relationship -- nothing about the actual User table is different or changed in any way. 但是,这纯粹是出于某种特定关系的目的-实际User表没有任何不同或以任何方式更改。

The point is that one table is created for User and this same table is used to store all User s no matter what context they were created in. You can import User into any and all of your apps, create as many and as varied relationships as you like and nothing really changes as far as User is concerned. 关键是为User创建了一个表,并且无论在哪个上下文中创建User ,该表都用于存储所有User 。您可以将User导入任何和所有应用中,创建尽可能多的关系。您喜欢,就User而言,什么都没有真正改变。

If the table name or something else does not fit in your needs you can always just extend the User model. 如果表名或其他名称不符合您的需求,则始终可以扩展User模型。

from django.contrib.auth.models import User

class Employee(User):
    ...

Any class extending Model class in models.py contributes to database schema. models.py任何扩展Model类的类都有助于数据库架构。 That means, django search your (and also django core) model.py files and looks for any class that extends Model like: 这意味着,django搜索您(以及django核心)的model.py文件,并查找扩展Model任何类,例如:

some models.py

class SomeModel(Model):
    ...
    ...

class Otherthing(Model):
    ...

that is also applies for django core code files. 这也适用于django核心代码文件。 Since all Database tables named using application label and model name, database ables created by django also have that... 由于所有使用应用程序标签和型号名称命名的数据库表,因此django创建的数据库功能也具有该功能...

For example, 例如,

from django.contrib.auth.models import User

If you track file hierarchy django -> contrib -> auth and open models.py file, you will see related model. 如果您跟踪文件层次结构django -> contrib -> auth并打开models.py文件,您将看到相关的模型。 Ther are also other Model classes in here, like Permission and Group models. 这也是这里的其他Model类,例如PermissionGroup模型。

Since these models are under auth application, database tables are auth_user , auth_perission and auth_group 由于这些模型在auth应用程序下,因此数据库表是auth_userauth_perissionauth_group

When you run manage.py syncdb command for the first time, django will create these tables... 首次运行manage.py syncdb命令时,django将创建这些表...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从django.contrib.auth.models导入用户 - from django.contrib.auth.models import User 引用从 django.contrib.auth.models 导入用户使用的 model 时,任务作业出错 - Error from Tasks job when referencing a model that uses from django.contrib.auth.models import User 问:Django 配置不当 - 从 django.contrib.auth.models 导入用户 - Q: Django ImproperlyConfigured - from django.contrib.auth.models import User Django:ImportError:无法从“django.contrib.auth.models”导入名称“get_user_model” - Django: ImportError: cannot import name 'get_user_model' from 'django.contrib.auth.models' 我不能在django.contrib.auth.models的用户模型中添加一些列吗? - Can't I add some columns to User model of django.contrib.auth.models? Django Import Class来自models.py - Django Import Class From models.py 在models.py中使用auth_user_model时Django request.user不起作用 - Django request.user not working when using auth_user_model in models.py 有关django.contrib.auth.models源代码的一些信息 - something about the django.contrib.auth.models source code 继承django.contrib.auth.models导致不对密码进行哈希处理 - Inheriting django.contrib.auth.models leads to not hashing passwords 我应该如何在django models.py上关联用户模型 - how should I relate user models on django models.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM