Is there a way to make Django get_or_create () to create an object without saving it to the database?

When using Django get_or_create () when created = True, is there a way to make it create an object without saving it to the database?

I want to take the newly created object, perform some checks, and only save it to the database if it passes all the tests.

+3
source share
2 answers

Instead of trying to force get_or_create to do something wrong, why not just create a new @classmethod method (or use a user manager) called get_or_new () and then only save () if you want?

+10
source

? .

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

Source: https://habr.com/ru/post/1725648/


All Articles