Search code examples
pythondjangocascading-deletessoft-delete

Django SafeDelete


I'm using the following library in my Django project https://pypi.org/project/django-safedelete/. All my models are setup for "cascading soft delete" and I'm having some trouble. One example of the problem I'm having:

In my application, I have a "vendor" model that has a foreign key to the main "user" model. These are also both set to cascading soft delete.

class VendorProfile(SafeDeleteModel):
    _safedelete_policy = SOFT_DELETE_CASCADE
    creation_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_time = models.DateTimeField(auto_now=True)

    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='vendor_profile')

The problem is this - let's say I go into the Django admin and delete a vendor model. It does NOT delete the associated user model, as it shouldn't. So said user still exists, the problem is anytime past that in code calls such as:

hasattr(user, "vendor_profile")

will return True even though the vendor profile was deleted.

The vendor profile objects are not visible in the admin and I'm quite confused why there's this discrepancy. Should I use a different library/make my own abstract model?

Thank you in advance!


Solution

  • Might be sub-optimal for you but you can check if VendorProfile's deleted DateTimefield provided by django-safedelete is not null.