Underoos
Underoos

Reputation: 5190

How to delete objects and their historical records at once in django?

I have an object called PTORequest which have a history field for storing HistoricalRecords of it.

history = HistoricalRecords()

I have not mentioned cascade_delete_history=True while creating history column which would automatically cascade history records.

How do I delete all the objects and their historical records from PTORequest table without doing model changes and migrations?

One thing I can think of is this:

for i in PTORequest.objects.all():
    print i.history.all().delete()
    print i.delete()

Is there any other efficient way to do this?

I'm using Django 1.11

Upvotes: 0

Views: 809

Answers (1)

iklinac
iklinac

Reputation: 15738

You can get all object historical records by Querying history on model class

So something like this:

PTORequest.history.all().delete()

Upvotes: 2

Related Questions