django-softdelete icon indicating copy to clipboard operation
django-softdelete copied to clipboard

Model.objects.get does not respect deleted_at field

Open davidhyman opened this issue 11 years ago • 2 comments

Calling .get on the manager still returns soft deleted records. For now, this is my workaround:

class BaseManager(SoftDeleteManager):
    def get(self, *pargs, **kwargs):
        return self.filter().get(*pargs, **kwargs)

The issue also affects get_or_create by virtue of it using the get method, causing integrity errors with unique constraints (not handled by the workaround). e.g. for MyModel already containing a soft deleted record 'foo':

MyModel.objects.get_or_create(unique_field1='foo')
  • get fails because it is now correctly ignoring the soft deleted record
  • create fails because there is already a 'foo' record for unique_field1

davidhyman avatar Aug 09 '13 15:08 davidhyman

Yes, that would be correct behavior. Soft delete isn't magic. If your design doesn't account for the fact that the keyspace will be partly occupied by hidden records, then the design will fail. Because soft-delete is about enabling restoring the record, if a new record occupies its keyspace, that restoration will fail. This behavior thus is precisely as expected. That said, the mixin for get is useful.

shayneoneill avatar Jan 22 '15 01:01 shayneoneill

@davidhyman I wouldn't have expected it to be applied automatically, please don't add this in. If you are using something like an API I expect the default to show all, allows you to apply filters for deleted or non-deleted items.

glynjackson avatar Sep 16 '16 11:09 glynjackson