RecycleBin() получил неожиданные аргументы ключевых слов: 'title', 'content_type', 'object_id', 'deleted_by'
В моем проекте Django есть модель мусорной корзины, которая прекрасно работает с другими моделями, поскольку я переопределил функцию delete для мягкого удаления любого объекта модели, используя is_deleted=True
в каждой модели.
class RecycleBin(models.Model):
title = models.CharField(
max_length=1000, verbose_name="Title", null=False, blank=False,
)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
deleted_object = GenericForeignKey("content_type", "object_id")
deleted_by = models.ForeignKey(
"Accounts.User",
verbose_name="Deleted By",
on_delete=models.CASCADE,
blank=False,
null=False,
related_name="deleted_by",
)
tenant = models.ForeignKey(
"Tenants.Tenant",
verbose_name="Tenant",
max_length=1000,
null=False,
blank=False,
on_delete=models.CASCADE,
)
date_deleted = models.DateTimeField(verbose_name="Date Deleted", auto_now_add=True)
due_deletion_date = models.DateTimeField(verbose_name="Due Deletion", default=None, null=True, editable=False)
Однако в моей модели Files, когда я пытаюсь удалить файл, я получаю ошибку RecycleBin() got unexpected keyword arguments: 'title', 'content_type', 'object_id', 'deleted_by'
.
class AllFilesManager(models.Manager):
def get_queryset(self):
return super().get_queryset()
class ActiveFileModelManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_deleted=False)
class File(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
name = models.CharField(max_length=1000, null=False, blank=False)
folder = models.ForeignKey(
"Folders.Folder", on_delete=models.CASCADE, null=False, blank=False
)
file = models.FileField(
upload_to='tenants',
storage=storage,
max_length=1000,
null=False, blank=False
)
is_deleted = models.BooleanField(default=False, verbose_name="Is Deleted?")
created_by = models.ForeignKey(
"Accounts.User",
verbose_name="Created By",
on_delete=models.CASCADE,
blank=False,
null=False,
related_name="file_created_by",
)
modified_by = models.ForeignKey(
"Accounts.User",
verbose_name="Modified By",
on_delete=models.CASCADE,
blank=True,
null=True,
related_name="file_modified_by",
)
tenant = models.ForeignKey(
"Tenants.Tenant",
verbose_name="Tenant",
max_length=1000,
on_delete=models.CASCADE,
null=False,
blank=False,
)
date_created = models.DateTimeField(verbose_name="Date Created", auto_now_add=True)
date_modified = models.DateTimeField(verbose_name="Date Modified", auto_now=True)
objects = ActiveFileModelManager()
all_objects = AllFilesManager()
def delete(self, using=None, keep_parents=False, user=None):
bin = RecycleBin.objects.create(
title=self.name,
content_type=ContentType.objects.get_for_model(self),
object_id=self.pk,
deleted_by=self.modified_by,
tenant=self.tenant,
)
if bin:
self.is_deleted = True
self.modified_by = user
self.save()
Вот функция удаления и из FileAPIView.
class FileAPIView(APIView):
def delete(self, request, pk=None):
ids = request.data.get("ids", [])
if len(ids) == 0:
try:
file = get_object_or_404(File, id=pk)
file.delete()
return JsonResponse(
{
"status": status.HTTP_204_NO_CONTENT,
"message": "File moved to the Recycle Bin successfully!",
}
)
except Exception as e:
return JsonResponse(
{
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
"message": "An error occurred while deleting the file!",
"results": {"error": str(e)},
}
)
else:
try:
for id in ids:
file = get_object_or_404(File, id=id)
file.delete()
return JsonResponse(
{
"status": status.HTTP_204_NO_CONTENT,
"message": "Files moved to the Recycle Bin successfully!",
}
)
except Exception as e:
return JsonResponse(
{
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
"message": "An error occurred while deleting the files!",
"results": {"error": str(e)},
}
)
Я пытался отладить это, но безуспешно, особенно учитывая, что это единичный случай только с одной моделью.