0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hsk81
  • 792
  • 8
  • 15
  • can you clarify what you mean by 'stops cascading for foreign keys'? what do you want to cascade, if it's not delete? – hwjp Oct 24 '11 at 04:56
  • E.g. if you delete a USER u Django normally propagates to all u.accounts of type ACCOUNT (at least in the admin interface), which then are also deleted (DELETE CASCADE in SQL). Well, similarly I'd like the delete date of my ACCOUNT (BASE) objects automatically set if I delete a corresponding USER object. – hsk81 Oct 24 '11 at 05:29
  • ok, so ACCOUNT(BASE) has an FK to User, and when you delete a User, you want it to cascade down to the related ACCOUNT object, but use your special delete method instead of actually deleting it... and that currently doesn't work? – hwjp Oct 24 '11 at 06:16
  • Exactly; but 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][1] implemented at [soft delete implementation][2]. I'll have a look at that; still thank you for the responses. :) [1]: http://stackoverflow.com/questions/6569121/django-soft-delete-doesnt-cascade-delete [2]: https://github.com/scoursen/django-softdelete – hsk81 Oct 24 '11 at 08:53

1 Answers1

0

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. :)

Community
  • 1
  • 1
hsk81
  • 792
  • 8
  • 15