[英]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
并运行syncdb
, User
和Group
的表就可以使用了。
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
模型中的一个模型执行类似创建ForeignKey
, OneToOneField
或ManyToManyField
的User
。 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类,例如Permission
和Group
模型。
Since these models are under auth
application, database tables are auth_user
, auth_perission
and auth_group
由于这些模型在auth
应用程序下,因此数据库表是auth_user
, auth_perission
和auth_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.