Django

Code

Changeset 3096

Show
Ignore:
Timestamp:
06/06/06 22:10:28 (2 years ago)
Author:
adrian
Message:

Added section to docs/db-api.txt get_or_create() section about GET vs. POST

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/db-api.txt

    r3092 r3096  
    716716created. 
    717717 
    718 This is meant as a shortcut to boilerplatish code. For example:: 
     718This is meant as a shortcut to boilerplatish code and is mostly useful for 
     719data-import scripts. For example:: 
    719720 
    720721    try: 
     
    748749use the result as the keyword arguments to the model class. 
    749750 
    750 Finally, if you have a field named ``defaults`` and want to use it as an exact 
    751 lookup in ``get_or_create()``, just use ``'defaults__exact'``, like so:: 
     751If you have a field named ``defaults`` and want to use it as an exact lookup in 
     752``get_or_create()``, just use ``'defaults__exact'``, like so:: 
    752753 
    753754    Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'}) 
     755 
     756Finally, a word on using ``get_or_create()`` in Django views. As mentioned 
     757earlier, ``get_or_create()`` is mostly useful in scripts that need to parse 
     758data and create new records if existing ones aren't available. But if you need 
     759to use ``get_or_create()`` in a view, please make sure to use it only in 
     760``POST`` requests unless you have a good reason not to. ``GET`` requests 
     761shouldn't have any effect on data; use ``POST`` whenever a request to a page 
     762has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec. 
     763 
     764.. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 
    754765 
    755766``count()``