Ticket #3121: get_or_create.diff
File get_or_create.diff, 3.7 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/related.py
226 226 return new_obj 227 227 create.alters_data = True 228 228 229 def get_or_create(self, **kwargs): 230 assert len(kwargs), 'get_or_create() must be passed at least one keyword argument' 231 defaults = kwargs.pop('defaults', {}) 232 try: 233 return self.get(**kwargs), False 234 except self.model.DoesNotExist: 235 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 236 params.update(defaults) 237 obj = self.create(**params) 238 return obj, True 239 get_or_create.alters_data = True 240 229 241 # remove() and clear() are only provided if the ForeignKey can have a value of null. 230 242 if rel_field.null: 231 243 def remove(self, *objs): … … 314 326 return new_obj 315 327 create.alters_data = True 316 328 329 def get_or_create(self, **kwargs): 330 assert len(kwargs), 'get_or_create() must be passed at least one keyword argument' 331 defaults = kwargs.pop('defaults', {}) 332 try: 333 return self.get(**kwargs), False 334 except self.model.DoesNotExist: 335 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) 336 params.update(defaults) 337 obj = self.create(**params) 338 return obj, True 339 get_or_create.alters_data = True 340 317 341 def _add_items(self, source_col_name, target_col_name, *objs): 318 342 # join_table: name of the m2m link table 319 343 # source_col_name: the PK colname in join_table for the source object -
tests/modeltests/get_or_create/models.py
15 15 def __unicode__(self): 16 16 return u'%s %s' % (self.first_name, self.last_name) 17 17 18 class Publisher(models.Model): 19 name = models.CharField(maxlength=100) 20 21 class Author(models.Model): 22 name = models.CharField(maxlength=100) 23 24 class Book(models.Model): 25 name = models.CharField(maxlength=100) 26 authors = models.ManyToManyField(Author, related_name='books') 27 publisher = models.ForeignKey(Publisher, related_name='books') 28 18 29 __test__ = {'API_TESTS':""" 19 30 # Acting as a divine being, create an Person. 20 31 >>> from datetime import date … … 49 60 False 50 61 >>> Person.objects.count() 51 62 2 63 64 # First create a Publisher. 65 >>> p = Publisher.objects.create(name='Acme Publishing') 66 67 # Create a book, it should be created. 68 >>> book, created = p.books.get_or_create(name='The Book of Ed & Fred') 69 >>> created 70 True 71 72 # Try again. 73 >>> book, created = p.books.get_or_create(name='The Book of Ed & Fred') 74 >>> created 75 False 76 77 # A book needs Authors. 78 >>> ed, created = book.authors.get_or_create(name='Ed') 79 >>> fred, created = book.authors.get_or_create(name='Fred') 80 81 # Add another Author, without any books. 82 >>> Author.objects.create(name='Ted') 83 <Author: Author object> 84 85 # By now, there should be 3 Authors in total. The `book' object should have 86 # two. 87 >>> Author.objects.count() 88 3 89 >>> book.authors.count() 90 2 91 92 # Ed writes another Book. 93 >>> ed.books.get_or_create(name="Ed's Recipies", publisher=p) 94 (<Book: Book object>, True) 95 96 # Now Ed has two Books, Fred just one. 97 >>> ed.books.count() 98 2 99 >>> fred.books.count() 100 1 52 101 """}