RecycleBin() got unexpected keyword arguments: 'title', 'content_type', 'object_id', 'deleted_by'
I have a recycle bin model in my Django project that works fine with other models since I have overidden the delete function to soft delete any model object by using is_deleted=True
in every model.
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)
However, in my Files model, when i try to delete a file, i get an error 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()
Here is the delete function from the FileAPIView as well.
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)},
}
)
I have tried debugging this to no success especially given its an isolated case with only one model.