Home > Back-end >  Getting error as -> 'NoneType' object has no attribute 'delete'
Getting error as -> 'NoneType' object has no attribute 'delete'

Time:09-23

I am trying to delete the "profiles" manually using "admin" portal of the DJANGO, but when I click on delete after selecting some profiles, I am getting an error as -> 'NoneType' object has no attribute 'delete'

I am using signals in my code.

signals.py code:-

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 code:-

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)

I don't know, why am I getting this error, can anyone figure it out?

CodePudding user response:

You are deleting a Profile, but that Profile has no linked user, since the user is NULL/None.

You can make your signal safer with:

def deleteUser(sender, instance, **kwargs):
    user = instance.user
    if user:
        user.delete()
  • Related