OneToOneField和删除 [英] OneToOneField and Deleting

查看:78
本文介绍了OneToOneField和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

 从django.db导入模型
from django.contrib.auth。模型导入用户


类配置文件(models.Model):
user = models.OneToOneField(User)
#...

def __unicode __(self):
return u'%s%s'%(self.user.first_name,self.user.last_name)

当使用Django管理员删除用户时,配置文件也被删除,这是我想要的。但是,当使用Django管理员删除配置文件时,用户不会 被删除,这不是我想要的。如何删除配置文件也会删除用户?

解决方案

由于 / code>链接到用户,它是关系中的依赖模型。因此,当您删除用户时,它将删除所有依赖的模型。但是,当您删除个人资料时,由于 User 不依赖个人资料,因此不会被删除。



不幸的是,根据 on_delete Django docs ,没有删除父关系的 on_delete 规则。为了做到这一点,您可以覆盖个人资料删除方法:

  class Profile(models.Model):
#...

def delete(self,* args,** kwargs)
self.user.delete()
return super(self .__ class__,self).delete(* args,** kwargs)
pre>

然后在做:

  Profile.objects.get( ...)delete()

还会删除个人资料的用户。然而,使用查询语言(这是Django Admin中调用的)删除配置文件时,不会调用 delete 方法,因为Django使用SQL DELETE批量删除对象: p>

  Profile.objects.filter(...)。delete()

在这种情况下,按照Django的建议 docs ,您将不得不使用 post_delete 信号( docs )。


$来自django.dispatch导入接收器的

$ d $ d
从django.db.models.signals导入post_delete

@receiver(post_delete,sender =个人资料)
def post_delete_user(sender,instance,* args,** kwargs):
if instance.user:#只是为了防止用户被指定
instance.user.delete()


I have the following model:

from django.db import models
from django.contrib.auth.models import User


class Profile(models.Model):
    user = models.OneToOneField(User)
    # ...

    def __unicode__(self):
        return u'%s %s' % (self.user.first_name, self.user.last_name)

When using the Django admin to delete the user, the profile gets deleted as well, which is what I want. However, when using the Django admin to delete the profile, the user does not get deleted, which is not what I want. How can I make it so that deleting the profile will also delete the user?

解决方案

Since Profile links to User, it is the dependent model in the relationship. Therefore when you delete a user, it deletes all dependent models. However when you delete a profile, since User does not depend on profile, it is not removed.

Unfortunately, according to on_delete Django docs, there is no on_delete rule which deletes the parent relations. In order to do that, you can overwrite the Profile's delete method:

class Profile(models.Model):
    # ...

    def delete(self, *args, **kwargs):
        self.user.delete()
        return super(self.__class__, self).delete(*args, **kwargs)

Then when doing:

Profile.objects.get(...).delete()

will also delete the profile's user. However the delete method will not be called when deleting profiles using querysets (which is what is called in Django Admin) since then Django uses SQL DELETE to delete objects in bulk:

Profile.objects.filter(...).delete()

In that case, as recommended by Django docs, you will have to use post_delete signal (docs).

from django.dispatch import receiver
from django.db.models.signals import post_delete

@receiver(post_delete, sender=Profile)
def post_delete_user(sender, instance, *args, **kwargs):
    if instance.user: # just in case user is not specified
        instance.user.delete()

这篇关于OneToOneField和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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