Here some models:
这里有些型号:
class UserProfile(models.Model):
name = models.CharField(max_length=30)
email = models.EmailField(unique=True, db_index=True)
birthday = models.DateField()
class Photo(models.Model):
user = models.ForeignKey(UserProfile)
description = models.TextField(blank=True)
photo = models.ImageField(upload_to='img/photo')
Let's say a user has 10 photos (10 objects of Photo
model). When user deletes himself will all those 10 Photo
database rows delete themselves automatically? (I've read docs, but English is not my native language, so I didn't understand everything about the on_delete
attribute.)
假设用户有10张照片(照片模型的10个对象)。当用户删除自己时,这10个照片数据库行会自动删除自己吗?(我读过文档,但英语不是我的母语,所以我不理解on_delete属性的所有内容。)
2 个解决方案
#1
15
that's the default behaviour, yes. you can change this behaviour with on_delete
这是默认行为。可以使用on_delete更改此行为
to get an illustration of this, try deleting a userprofile in the admin. it first shows a warning page, listing all related objects that will also get deleted
为了说明这一点,可以尝试在admin中删除一个userprofile。它首先显示一个警告页面,列出所有将被删除的相关对象
#2
157
I'll go through the values for on_delete
as they apply to this case. As it notes in the docs, these are all in that models
module, so you'd use it as models.ForeignKey(UserProfile, on_delete=models.CASCADE)
, etc.
我将检查on_delete的值,因为它们适用于本例。正如它在文档中所指出的,这些都在模型模块中,所以您可以将其用作模型。ForeignKey(UserProfile on_delete = models.CASCADE),等等。
These rules apply however you delete an object, whether you do it in the admin panel or working directly with the Model
instance. (But it won't take effect if you work directly with the underlying database in SQL.)
无论您是在管理面板中执行,还是直接使用模型实例,这些规则都适用于删除对象。(但如果直接使用SQL中的底层数据库,则不会生效。)
-
CASCADE
: when you delete theUserProfile
, all relatedPhoto
s will be deleted too. This is the default. (So in answer to that aspect of your question, yes, if you delete your user account the photos will be deleted automatically.)级联:当您删除UserProfile时,所有相关的照片也会被删除。这是默认的。(所以为了回答你的问题,是的,如果你删除了你的用户账号,照片会自动删除。)
-
PROTECT
: this will stop you from deleting aUserProfile
with relatedPhoto
s, raising adjango.db.models.ProtectedError
if you try. The idea would be that the user would need to disassociate or delete allPhoto
s before they could delete their profile.保护:这将阻止您删除带有相关照片的用户配置文件,从而生成一个django.db.models。ProtectedError如果你试一试。这样做的目的是,用户需要在删除自己的个人资料之前解除关联或删除所有的照片。
-
SET_NULL
: when you delete theUserProfile
, all associatedPhoto
s will still exist but will no longer be associated with anyUserProfile
. This would requirenull=True
in theForeignKey
definition.SET_NULL:当您删除UserProfile时,所有关联的照片仍然存在,但是不再与任何UserProfile关联。这将要求在ForeignKey定义中null=True。
-
SET_DEFAULT
: when you delete theUserProfile
, all associatedPhoto
s will be changed to point to their defaultUserProfile
as specified by thedefault
attribute in theForeignKey
definition (you could use this to pass "orphaned" photos off to a certain user - but this isn't going to be common,SET_NULL
orSET()
will be much more common)SET_DEFAULT:当你删除UserProfile,所有相关的照片会被修改指向他们的默认UserProfile ForeignKey定义中指定的默认属性(你可以使用这种通过“孤儿”照片一个特定的用户——但这并不常见,SET_NULL或一组()将更为常见)
-
SET()
: when you delete theUserProfile
, the target of thePhoto
s'ForeignKey
will be set to the value passed in to theSET
function, or what it returns if it is a callable. (Sorry, I haven't explained that well, but the docs have an example which explains better.)SET():当您删除UserProfile时,Photos' ForeignKey的目标将被设置为传递给SET函数的值,如果是可调用的,则返回的值。(对不起,我没解释清楚,但文档中有一个例子可以更好地解释。)
-
DO_NOTHING
: when you delete theUserProfile
, all relatedPhoto
s will remain unaltered, thus having a broken reference, unless you have used some other SQL to take care of it.DO_NOTHING:当您删除UserProfile时,所有相关的照片将保持不变,从而有一个坏引用,除非您使用了其他SQL来处理它。
(Also, on_delete
isn't a method. It's an attribute of the ForeignKey
field.)
另外,on_delete不是一个方法。它是ForeignKey字段的属性。
#1
15
that's the default behaviour, yes. you can change this behaviour with on_delete
这是默认行为。可以使用on_delete更改此行为
to get an illustration of this, try deleting a userprofile in the admin. it first shows a warning page, listing all related objects that will also get deleted
为了说明这一点,可以尝试在admin中删除一个userprofile。它首先显示一个警告页面,列出所有将被删除的相关对象
#2
157
I'll go through the values for on_delete
as they apply to this case. As it notes in the docs, these are all in that models
module, so you'd use it as models.ForeignKey(UserProfile, on_delete=models.CASCADE)
, etc.
我将检查on_delete的值,因为它们适用于本例。正如它在文档中所指出的,这些都在模型模块中,所以您可以将其用作模型。ForeignKey(UserProfile on_delete = models.CASCADE),等等。
These rules apply however you delete an object, whether you do it in the admin panel or working directly with the Model
instance. (But it won't take effect if you work directly with the underlying database in SQL.)
无论您是在管理面板中执行,还是直接使用模型实例,这些规则都适用于删除对象。(但如果直接使用SQL中的底层数据库,则不会生效。)
-
CASCADE
: when you delete theUserProfile
, all relatedPhoto
s will be deleted too. This is the default. (So in answer to that aspect of your question, yes, if you delete your user account the photos will be deleted automatically.)级联:当您删除UserProfile时,所有相关的照片也会被删除。这是默认的。(所以为了回答你的问题,是的,如果你删除了你的用户账号,照片会自动删除。)
-
PROTECT
: this will stop you from deleting aUserProfile
with relatedPhoto
s, raising adjango.db.models.ProtectedError
if you try. The idea would be that the user would need to disassociate or delete allPhoto
s before they could delete their profile.保护:这将阻止您删除带有相关照片的用户配置文件,从而生成一个django.db.models。ProtectedError如果你试一试。这样做的目的是,用户需要在删除自己的个人资料之前解除关联或删除所有的照片。
-
SET_NULL
: when you delete theUserProfile
, all associatedPhoto
s will still exist but will no longer be associated with anyUserProfile
. This would requirenull=True
in theForeignKey
definition.SET_NULL:当您删除UserProfile时,所有关联的照片仍然存在,但是不再与任何UserProfile关联。这将要求在ForeignKey定义中null=True。
-
SET_DEFAULT
: when you delete theUserProfile
, all associatedPhoto
s will be changed to point to their defaultUserProfile
as specified by thedefault
attribute in theForeignKey
definition (you could use this to pass "orphaned" photos off to a certain user - but this isn't going to be common,SET_NULL
orSET()
will be much more common)SET_DEFAULT:当你删除UserProfile,所有相关的照片会被修改指向他们的默认UserProfile ForeignKey定义中指定的默认属性(你可以使用这种通过“孤儿”照片一个特定的用户——但这并不常见,SET_NULL或一组()将更为常见)
-
SET()
: when you delete theUserProfile
, the target of thePhoto
s'ForeignKey
will be set to the value passed in to theSET
function, or what it returns if it is a callable. (Sorry, I haven't explained that well, but the docs have an example which explains better.)SET():当您删除UserProfile时,Photos' ForeignKey的目标将被设置为传递给SET函数的值,如果是可调用的,则返回的值。(对不起,我没解释清楚,但文档中有一个例子可以更好地解释。)
-
DO_NOTHING
: when you delete theUserProfile
, all relatedPhoto
s will remain unaltered, thus having a broken reference, unless you have used some other SQL to take care of it.DO_NOTHING:当您删除UserProfile时,所有相关的照片将保持不变,从而有一个坏引用,除非您使用了其他SQL来处理它。
(Also, on_delete
isn't a method. It's an attribute of the ForeignKey
field.)
另外,on_delete不是一个方法。它是ForeignKey字段的属性。