Django - 管理区域 - 我无法从用户中删除用户(用户匹配查询不存在。)

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

我创建了一个用户应用程序。在模型中,我创建了一个配置文件和信号。在管理区域,我可以创建一个新的用户端,也可以创建一个配置文件。当我删除新用户表单时,配置文件也从用户中删除并且没问题。但是,如果我尝试从用户区域删除新用户,我会收到错误消息:“DoesNotExist at /admin/auth/user/3/delete/ - 用户匹配查询不存在”。我不知道为什么?有什么建议吗?

我 运行 并应用了所有迁移,我还删除并创建了另一个数据库,但没有解决这个问题。

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

在您的个人资料模型中,您有:

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

with on_delete=models.CASCADE models.CASCADE 已经在删除一个实例时删除实例,并且在您的 signals.py 中您有:

post_delete.connect(stergeUser, sender=Profil)

可能当您删除用户时,django 也会删除 Profile 实例,触发 post_delete 信号。

您可以尝试删除 post_delete 信号,删除操作会正常工作。