开发者

Is there any way to make Django's get_or_create() to create an object without saving it to database?

When using Django's get_or_create(), when created=True, is th开发者_如何学Goere any way to make it so that it creates an object without saving it to DB?

I want to take the newly created object, do some validation tests, and only save it to DB if it passes all tests.


Rather than try to make get_or_create something it's not, why not just create a new @classmethod (or use a custom manager) called get_or_new(), and then only do the save() when you want to?


Why not override the model's save method, and have it do the tests there? Then you don't have to make sure you use the right creation method each time.

class MyModel(Model)
    def save(self):
        self.run_presave_tests()
        if self.passed_tests:
            super(MyModel, self).save()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜