Ticket #8419: fix_get_or_create_race.v2.diff
File fix_get_or_create_race.v2.diff, 1.5 KB (added by , 16 years ago) |
---|
-
django/db/models/query.py
328 328 params.update(defaults) 329 329 obj = self.model(**params) 330 330 sid = transaction.savepoint() 331 obj.save( )331 obj.save(force_insert=True) 332 332 transaction.savepoint_commit(sid) 333 333 return obj, True 334 334 except IntegrityError, e: -
docs/ref/models/querysets.txt
577 577 obj = Person.objects.get(first_name='John', last_name='Lennon') 578 578 except Person.DoesNotExist: 579 579 obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)) 580 obj.save( )580 obj.save(force_insert=True) 581 581 582 582 This pattern gets quite unwieldy as the number of fields in a model goes up. 583 583 The above example can be rewritten using ``get_or_create()`` like so:: … … 596 596 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 597 597 params.update(defaults) 598 598 obj = self.model(**params) 599 obj.save( )599 obj.save(force_insert=True) 600 600 601 601 In English, that means start with any non-``'defaults'`` keyword argument that 602 602 doesn't contain a double underscore (which would indicate a non-exact lookup).