Ticket #7402: get_or_create.diff

File get_or_create.diff, 963 bytes (added by Julian Bez, 16 years ago)

Patch from nullie as proper .diff

  • django/db/models/query.py

     
    330330                obj.save()
    331331                return obj, True
    332332            except IntegrityError, e:
    333                 return self.get(**kwargs), False
     333                # If transactions are managed manually, we must fail.
     334                if transaction.is_managed():
     335                    raise e
     336                # Try to get object, maybe this was just concurrency error.
     337                try:
     338                    transaction.rollback_unless_managed()
     339                    obj = self.get(**kwargs)
     340                except self.model.DoesNotExist:
     341                     # No object found, re-raise error.
     342                     raise e
     343                return obj, False
    334344
    335345    def latest(self, field_name=None):
    336346        """
Back to Top