Does get_or_create() have to save right away? (Django)

You can just do:

try:
    obj = Model.objects.get(**kwargs)
except Model.DoesNotExist:
    obj = Model(**dict((k,v) for (k,v) in kwargs.items() if '__' not in k))

which is pretty much what get_or_create does, sans commit.

Leave a Comment