Changeset 8670
- Timestamp:
- 08/28/08 12:18:05 (3 months ago)
- Files:
-
- django/trunk/django/db/models/query.py (modified) (2 diffs)
- django/trunk/docs/ref/models/instances.txt (modified) (1 diff)
- django/trunk/docs/ref/models/querysets.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/query.py
r8598 r8670 309 309 """ 310 310 obj = self.model(**kwargs) 311 obj.save( )311 obj.save(force_insert=True) 312 312 return obj 313 313 … … 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 django/trunk/docs/ref/models/instances.txt
r8506 r8670 194 194 auto-primary-key values`_ above and `Forcing an INSERT or UPDATE`_ below. 195 195 196 .. _ref-models-force-insert: 197 196 198 Forcing an INSERT or UPDATE 197 199 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ django/trunk/docs/ref/models/querysets.txt
r8658 r8670 557 557 558 558 p = Person(first_name="Bruce", last_name="Springsteen") 559 p.save( )559 p.save(force_insert=True) 560 560 561 561 are equivalent. 562 563 The :ref:`force_insert <ref-models-force-insert>` parameter is documented 564 elsewhere, but all it means is that a new object will always be created. 565 Normally you won't need to worry about this. However, if your model contains a 566 manual primary key value that you set and if that value already exists in the 567 database, a call to ``create()`` will fail with an ``IntegrityError`` since 568 primary keys must be unique. So remember to be prepared to handle the 569 exception if you are using manual primary keys. 562 570 563 571 ``get_or_create(**kwargs)`` … … 591 599 is *not* found, ``get_or_create()`` will instantiate and save a new object, 592 600 returning a tuple of the new object and ``True``. The new object will be 593 created according to this algorithm::601 created roughly according to this algorithm:: 594 602 595 603 defaults = kwargs.pop('defaults', {}) … … 602 610 doesn't contain a double underscore (which would indicate a non-exact lookup). 603 611 Then add the contents of ``defaults``, overriding any keys if necessary, and 604 use the result as the keyword arguments to the model class. 612 use the result as the keyword arguments to the model class. As hinted at 613 above, this is a simplification of the algorithm that is used, but it contains 614 all the pertinent details. The internal implementation has some more 615 error-checking than this and handles some extra edge-conditions; if you're 616 interested, read the code. 605 617 606 618 If you have a field named ``defaults`` and want to use it as an exact lookup in … … 608 620 609 621 Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'}) 622 623 624 The ``get_or_create()`` method has similar error behaviour to ``create()`` 625 when you are using manually specified primary keys. If an object needs to be 626 created and the key already exists in the database, an ``IntegrityError`` will 627 be raised. 610 628 611 629 Finally, a word on using ``get_or_create()`` in Django views. As mentioned
