1

When I delete records of table-A, I'd like to move the rows to table-A-history table.

Is it suffice to override manager's delete() method to actually move and delete?

All the soft delete I've been able to find uses bool flag to indicate a soft-delete.

2 Answers 2

3

Yes and no.

If you override the delete() method then it will be called every time you delete single object:

my_obj.delete()

But this method will not called on bulk queryset's delete().

MyModel.objects.filter(some=value).delete()

I suspect that you can create for this model the custom QuerySet and override it's delete() method.

Sign up to request clarification or add additional context in comments.

4 Comments

you mean when I override the queryset or manager's delete, It wouldn't affect the obj.delete(). and I'd need to override model's delete as well for it?
I am not 100% sure but AFAIK instance.delete() doesn't use the model's queryset. So I think yes, you have to override both delete() methods.
I've been using manager method to define a method which doesn't return a queryset. (such as boolean). delete() seems unique in a sense, it returns none() queryset. What's the difference between the queryset method vs manager method? maybe I should ask this in a separate question..
As far as I understand there is no difference between manager methods and queryset methods. Moreover, you can create a manager from the queryset using the as_manager() method: docs.djangoproject.com/en/1.7/topics/db/managers/…
1

I have never actually implemented a feature such as the one you are attempting to make, but I would assume overriding a core feature may not be the best idea, especially as it may become a bit confusing for you. My suggestion would be to create a custom delete method as follows:

class Destination(modelsModel):
    title = models.Charfield(max_length=9000)
    [...]

class ItemToMove(models.Model):
    title = models.CharField(max_length=9000)
    [...same fields as "Destination" model...]

    def delete_and_move(self):
        Destination.objects.create(title=self.title, [...])
        self.delete()
        return "Object successfully deleted and moved"

    def __unicode__(self):
        return self.title

You can use this to delete a single object by typing obj.delete_and_move(), or you can even run items = ItemToMove.objects.filter(title='barney'), and then delete via:

for i in items:
    i.delete_and_move()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.