#10148: get_or_create function documentation is not clear enough
------------------------------------+---------------------------------------
          Reporter:  batiste        |         Owner:  nobody                    
  
            Status:  closed         |     Milestone:                            
  
         Component:  Documentation  |       Version:  1.0                       
  
        Resolution:  invalid        |      Keywords:  get_or_create, 
documentation
             Stage:  Unreviewed     |     Has_patch:  0                         
  
        Needs_docs:  0              |   Needs_tests:  0                         
  
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Changes (by arien):

  * status:  new => closed
  * resolution:  => invalid

Comment:

 [http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-
 kwargs The documentation on get_or_create] says:

 {{{
     Any keyword arguments passed to ``get_or_create()`` -- *except* an
 optional one
     called ``defaults`` -- will be used in a ``get()`` call. If an object
 is found,
     ``get_or_create()`` returns a tuple of that object and ``False``. If
 an object
     is *not* found, ``get_or_create()`` will instantiate and save a new
 object,
     returning a tuple of the new object and ``True``.
 }}}

 (It also shows an example of using ``get_or_create`` in terms of ``get``
 and ``create``.)

 So, in your example, ``get_or_create`` looks for a ``Game`` object with
 ``id=116``, one is found and returned "as-is".

 By the way, the "better way" you mention:

 {{{
 #!python
 g, c = Game.objects.get_or_create(id=116, **defaults)
 }}}

 will look up a ``Game`` object with ``id=116`` and
 ``start_date=datetime.datetime(2009, 4, 18, 16, 1)``.  This object isn't
 found, so one will be created.  The result is an ``IntegrityError``, since
 you're trying to use the same primary key for more than one object.

 Closing as invalid since this seems to be clearly documented in both
 English and Python.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/10148#comment:3>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to