开发者

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 overrid开发者_运维百科ing the model save() method but I don't know how to create a new instance without creating a infinite save() loop.

Thanks


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜