Ticket #8419: fix_get_or_create_race.diff
File fix_get_or_create_race.diff, 1.5 KB (added by , 16 years ago) |
---|
-
django/db/models/query.py
327 327 params.update(defaults) 328 328 obj = self.model(**params) 329 329 sid = transaction.savepoint() 330 obj.save( )330 obj.save(force_insert=True) 331 331 transaction.savepoint_commit(sid) 332 332 return obj, True 333 333 except IntegrityError, e: -
docs/db-api.txt
1174 1174 obj = Person.objects.get(first_name='John', last_name='Lennon') 1175 1175 except Person.DoesNotExist: 1176 1176 obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)) 1177 obj.save( )1177 obj.save(force_insert=True) 1178 1178 1179 1179 This pattern gets quite unwieldy as the number of fields in a model goes up. 1180 1180 The above example can be rewritten using ``get_or_create()`` like so:: … … 1193 1193 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 1194 1194 params.update(defaults) 1195 1195 obj = self.model(**params) 1196 obj.save( )1196 obj.save(force_insert=True) 1197 1197 1198 1198 In English, that means start with any non-``'defaults'`` keyword argument that 1199 1199 doesn't contain a double underscore (which would indicate a non-exact lookup).