Ticket #8419: fix_get_or_create_race.diff

File fix_get_or_create_race.diff, 1.5 KB (added by Richard Davies <richard.davies@…>, 16 years ago)

Same patch as before, now in correct Django patch style

  • django/db/models/query.py

     
    327327                params.update(defaults)
    328328                obj = self.model(**params)
    329329                sid = transaction.savepoint()
    330                 obj.save()
     330                obj.save(force_insert=True)
    331331                transaction.savepoint_commit(sid)
    332332                return obj, True
    333333            except IntegrityError, e:
  • docs/db-api.txt

     
    11741174        obj = Person.objects.get(first_name='John', last_name='Lennon')
    11751175    except Person.DoesNotExist:
    11761176        obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    1177         obj.save()
     1177        obj.save(force_insert=True)
    11781178
    11791179This pattern gets quite unwieldy as the number of fields in a model goes up.
    11801180The above example can be rewritten using ``get_or_create()`` like so::
     
    11931193    params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
    11941194    params.update(defaults)
    11951195    obj = self.model(**params)
    1196     obj.save()
     1196    obj.save(force_insert=True)
    11971197
    11981198In English, that means start with any non-``'defaults'`` keyword argument that
    11991199doesn't contain a double underscore (which would indicate a non-exact lookup).
Back to Top