Ticket #8419: fix_get_or_create_race.v2.diff

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

Same patch as before, updated for the docs refactoring of r8506

  • django/db/models/query.py

     
    328328                params.update(defaults)
    329329                obj = self.model(**params)
    330330                sid = transaction.savepoint()
    331                 obj.save()
     331                obj.save(force_insert=True)
    332332                transaction.savepoint_commit(sid)
    333333                return obj, True
    334334            except IntegrityError, e:
  • docs/ref/models/querysets.txt

     
    577577        obj = Person.objects.get(first_name='John', last_name='Lennon')
    578578    except Person.DoesNotExist:
    579579        obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    580         obj.save()
     580        obj.save(force_insert=True)
    581581
    582582This pattern gets quite unwieldy as the number of fields in a model goes up.
    583583The above example can be rewritten using ``get_or_create()`` like so::
     
    596596    params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
    597597    params.update(defaults)
    598598    obj = self.model(**params)
    599     obj.save()
     599    obj.save(force_insert=True)
    600600
    601601In English, that means start with any non-``'defaults'`` keyword argument that
    602602doesn't contain a double underscore (which would indicate a non-exact lookup).
Back to Top