djangocascadestamp

Delete using a "delete stamp" and auto cascading


Since I never want to actually delete my USER (BASE) objects, I've introduced a delete_date on my BASE model:

class BASE (models.Model):

    class Meta:

        abstract = True
        app_label = 'base'
        verbose_name_plural = 'bases'

    objects = BASE_MANAGER ()

    insert_date = models.DateTimeField (default = datetime.now (), auto_now_add = True)
    update_date = models.DateTimeField (default = datetime.now (), auto_now = True)
    delete_date = models.DateTimeField (null = True, blank = True)

    def save (self):

        super (BASE, self).save ()

    def delete (self):

        if not self.delete_date:
            self.update_date = datetime.now ()
            self.delete_date = datetime.now ()
            self.save ()
        else:
            pass ## no delete!

Now since I've not included super (BASE, self).delete () in BASE.delete no actual SQL is performed, which is fine; unfortunately this also stops the cascading for foreign keys. Is there an elegant way to achieve this in Django?


Solution

  • I think I've found the answer I've been looking for, since apparently the actual term that describes my use case is "soft delete" described in soft delete cascading implemented at soft delete implementation.

    I'll have a look at that; still thank you for the responses. :)