When using Django's get_or_create(), when created=True, is there 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With