Ticket #9029: 9029-get-or-create-r12805.diff
File 9029-get-or-create-r12805.diff, 1.7 KB (added by , 15 years ago) |
---|
-
django/db/models/query.py
359 359 assert kwargs, \ 360 360 'get_or_create() must be passed at least one keyword argument' 361 361 defaults = kwargs.pop('defaults', {}) 362 lookup = kwargs.copy() 363 for f in self.model._meta.fields: 364 if f.column in lookup: 365 lookup[f.name] = lookup.pop(f.column) 362 366 try: 363 367 self._for_write = True 364 return self.get(** kwargs), False368 return self.get(**lookup), False 365 369 except self.model.DoesNotExist: 366 370 try: 367 371 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) … … 374 378 except IntegrityError, e: 375 379 transaction.savepoint_rollback(sid, using=self.db) 376 380 try: 377 return self.get(** kwargs), False381 return self.get(**lookup), False 378 382 except self.model.DoesNotExist: 379 383 raise e 380 384 -
tests/regressiontests/get_or_create_regress/models.py
38 38 >>> p.books.count() 39 39 1 40 40 41 # Use the publisher's primary key instead of a model instance. 42 >>> book, created = Book.objects.get_or_create(publisher_id=p.id, name='The Great Book of Ed') 43 >>> created 44 True 45 >>> book.publisher == p 46 True 47 41 48 # 42 49 # ManyRelatedManager 43 50 #