获取错误,因为 -> 'NoneType' 对象没有属性 'delete'
Getting error as -> 'NoneType' object has no attribute 'delete'
我正在尝试使用 DJANGO 的“管理”门户手动删除“配置文件”,但是当我在选择一些配置文件后单击删除时,出现错误 -> 'NoneType'对象没有属性'delete'
我在我的代码中使用了信号。
signals.py代码:-
from .models import Profile
from django.contrib.auth.models import User
from django.db.models.signals import post_save, post_delete
def createProfile(sender, instance, created, **kwargs):
if created:
user = instance
profile = Profile.objects.create(
user = user,
username = user.username,
email = user.email,
name = user.first_name,
)
def updateUser(sender, instance, created, **kwargs):
profile = instance
user = profile.user
if created == False:
user.first_name = profile.name
user.username = profile.username
user.email = profile.email
user.save()
def deleteUser(sender, instance, **kwargs):
user = instance.user
user.delete()
post_save.connect(createProfile, sender = User)
post_save.connect(updateUser, sender = Profile)
post_delete.connect(deleteUser, sender = Profile)
models.py代码:-
from django.db import models
from django.contrib.auth.models import User
import uuid
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, blank = True, null = True)
location = models.CharField(max_length=200, blank = True, null = True)
username = models.CharField(max_length=200, blank = True, null = True)
email = models.EmailField(max_length=500, blank=True, null = False)
short_intro = models.CharField(max_length=200, blank=True, null=True)
bio = models.TextField(blank=True, null=True)
profile_image = models.ImageField(null = True, blank = True, upload_to = 'profiles/', default = 'profiles/user-default.png')
social_github = models.CharField(max_length=200, blank = True, null = True)
social_twitter = models.CharField(max_length=200, blank = True, null = True)
social_linkedin = models.CharField(max_length=200, blank = True, null = True)
social_youtube = models.CharField(max_length=200, blank = True, null = True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return str(self.username)
class Skill(models.Model):
owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null = True, blank = True)
name = models.CharField(max_length=200, null=True, blank = True)
description = models.TextField(null = True, blank = True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return str(self.name)
我不知道,为什么会出现此错误,有人能解决吗?
您正在删除 Profile
,但 Profile
没有链接用户,因为 user
是 NULL
/None
。
您可以通过以下方式使信号更安全:
def deleteUser(sender, instance, **kwargs):
user = instance.user
<strong>if user</strong>:
user.delete()
我正在尝试使用 DJANGO 的“管理”门户手动删除“配置文件”,但是当我在选择一些配置文件后单击删除时,出现错误 -> 'NoneType'对象没有属性'delete'
我在我的代码中使用了信号。
signals.py代码:-
from .models import Profile
from django.contrib.auth.models import User
from django.db.models.signals import post_save, post_delete
def createProfile(sender, instance, created, **kwargs):
if created:
user = instance
profile = Profile.objects.create(
user = user,
username = user.username,
email = user.email,
name = user.first_name,
)
def updateUser(sender, instance, created, **kwargs):
profile = instance
user = profile.user
if created == False:
user.first_name = profile.name
user.username = profile.username
user.email = profile.email
user.save()
def deleteUser(sender, instance, **kwargs):
user = instance.user
user.delete()
post_save.connect(createProfile, sender = User)
post_save.connect(updateUser, sender = Profile)
post_delete.connect(deleteUser, sender = Profile)
models.py代码:-
from django.db import models
from django.contrib.auth.models import User
import uuid
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=200, blank = True, null = True)
location = models.CharField(max_length=200, blank = True, null = True)
username = models.CharField(max_length=200, blank = True, null = True)
email = models.EmailField(max_length=500, blank=True, null = False)
short_intro = models.CharField(max_length=200, blank=True, null=True)
bio = models.TextField(blank=True, null=True)
profile_image = models.ImageField(null = True, blank = True, upload_to = 'profiles/', default = 'profiles/user-default.png')
social_github = models.CharField(max_length=200, blank = True, null = True)
social_twitter = models.CharField(max_length=200, blank = True, null = True)
social_linkedin = models.CharField(max_length=200, blank = True, null = True)
social_youtube = models.CharField(max_length=200, blank = True, null = True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return str(self.username)
class Skill(models.Model):
owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null = True, blank = True)
name = models.CharField(max_length=200, null=True, blank = True)
description = models.TextField(null = True, blank = True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return str(self.name)
我不知道,为什么会出现此错误,有人能解决吗?
您正在删除 Profile
,但 Profile
没有链接用户,因为 user
是 NULL
/None
。
您可以通过以下方式使信号更安全:
def deleteUser(sender, instance, **kwargs):
user = instance.user
<strong>if user</strong>:
user.delete()