Home > Blockchain >  Django - Admin Area - I can't delete a user from users (User matching query does not exist.)
Django - Admin Area - I can't delete a user from users (User matching query does not exist.)

Time:04-07

I created a Users app. In models i create a Profile and signals. In admin area i can create a new user end is created a Profile as well. When i delete the new user form Profile is also deleted from Users and is ok. But if i try to delete the new user from Users area i got error message: "DoesNotExist at /admin/auth/user/3/delete/ - User matching query does not exist". I dont know why ? Any suggestion ?

I run and apply all the migrations and also i delete and create another database but is not solve this issue.

models.py

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.

class Profil(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    username = models.CharField(max_length=200, blank=True, null=True)
    nume = models.CharField(max_length=200, blank=True, null=True)
    avatar = models.ImageField ( upload_to ='utilizatori/', default='utilizatori/avatar_profilearning.jpg', verbose_name='Avatar (320px * 320px)')
    departament = models.CharField(max_length=200, blank=True, null=True)
    functie = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(max_length=500, blank=True, null=True)
    descriere = RichTextUploadingField(external_plugin_resources=[( 'emojione', '/static/src/plugins/ckeditor_plugins/emojione/' , 'plugin.js', )], config_name='default')
    creat = models.DateTimeField(auto_now_add=True)

    #pentru a redimensiona rezolutia avatarului incarcat
    def save(self, *args, **kawrgs):
        super().save(*args, **kawrgs)

        img = Image.open(self.avatar.path)

        if img.height > 320 or img.width > 320:
            output_size = (320, 320)
            img.thumbnail(output_size)
            img.save(self.avatar.path)
    
    def __str__(self):
        return str(self.user.username)

    class Meta:
        verbose_name = 'Profil'
        verbose_name_plural = 'Profil'

signals.py

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

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

def creareProfil(sender, instance, created, **kawrgs):

    if created:
        user = instance
        profil = Profil.objects.create(
            user=user,
            username=user.username,
            email=user.email,
            nume=user.first_name,
        )

def stergeUser(sender, instance, **kawrgs):
    user = instance.user
    user.delete()

post_save.connect(creareProfil, sender=User)
post_delete.connect(stergeUser, sender=Profil)

apps.py

from django.apps import AppConfig


class UtilizatoriConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'utilizatori'

    def ready(self):
        import utilizatori.signals

CodePudding user response:

In your Profile model you have:

user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)

with on_delete=models.CASCADE the models.CASCADE already delete the instance when one is deleted and in your signals.py you have:

post_delete.connect(stergeUser, sender=Profil)

probably when you delete the user django delete the Profile instance as well, trigging the post_delete signal.

You can try remove the post_delete signal and the delete action will work fine.

  • Related