pablo
pablo

Reputation: 2809

create a new model instance version instead of update

I have a model with a version field - autocreate timestamp. When a model instance is being saved I want to create a new instance with a new timestamp instead of updating the old model instance.

Is it possible?

I thought of overriding the model save() method but I don't know how to create a new instance without creating a infinite save() loop.

Thanks

Upvotes: 1

Views: 727

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You could set self.id = None in the overridden save method - then in the super method, Django would do an INSERT rather than an UPDATE.

Or, as pointed out in the documentation here, you could use the force_insert=True parameter in the call to save, which does the same thing.

Upvotes: 3

Related Questions