Ticket #16865: patch_16865_get_or_create_on_multidb_stuff.diff

File patch_16865_get_or_create_on_multidb_stuff.diff, 2.4 KB (added by noria, 13 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 41c24c7..a2f3f5d 100644
    a b class QuerySet(object):  
    438438            if f.attname in lookup:
    439439                lookup[f.name] = lookup.pop(f.attname)
    440440        try:
    441             self._for_write = True
    442441            return self.get(**lookup), False
    443442        except self.model.DoesNotExist:
    444443            try:
    445                 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
    446                 params.update(defaults)
    447                 obj = self.model(**params)
    448                 sid = transaction.savepoint(using=self.db)
    449                 obj.save(force_insert=True, using=self.db)
    450                 transaction.savepoint_commit(sid, using=self.db)
    451                 return obj, True
    452             except IntegrityError, e:
    453                 transaction.savepoint_rollback(sid, using=self.db)
    454                 exc_info = sys.exc_info()
     444                read_db = self.db
     445                self._for_write = True
     446                if read_db == self.db:
     447                    raise self.model.DoesNotExist
     448                return self.get(**lookup), False
     449            except self.model.DoesNotExist:
    455450                try:
    456                     return self.get(**lookup), False
    457                 except self.model.DoesNotExist:
    458                     # Re-raise the IntegrityError with its original traceback.
    459                     raise exc_info[1], None, exc_info[2]
     451                    params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
     452                    params.update(defaults)
     453                    obj = self.model(**params)
     454                    sid = transaction.savepoint(using=self.db)
     455                    obj.save(force_insert=True, using=self.db)
     456                    transaction.savepoint_commit(sid, using=self.db)
     457                    return obj, True
     458                except IntegrityError, e:
     459                    transaction.savepoint_rollback(sid, using=self.db)
     460                    exc_info = sys.exc_info()
     461                    try:
     462                        return self.get(**lookup), False
     463                    except self.model.DoesNotExist:
     464                        # Re-raise the IntegrityError with its original traceback.
     465                        raise exc_info[1], None, exc_info[2]
    460466
    461467    def latest(self, field_name=None):
    462468        """
Back to Top