Django

Code

Ticket #5068: 5068.2.patch

File 5068.2.patch, 1.2 kB (added by SmileyChris, 1 year ago)
  • docs/db-api.txt

    old new  
    207207 
    208208The ``save()`` method has no return value. 
    209209 
    210 Updating ``ForeignKey`` fields works exactly the same way; simply assign an 
    211 object of the right type to the field in question:: 
     210Saving ForeignKey and ManyToManyField fields 
     211-------------------------------------------- 
    212212 
     213Updating ``ForeignKey`` fields works exactly the same way as saving a normal 
     214field; simply assign an object of the right type to the field in question::  
     215 
     216    cheese_blog = Blog.objects.get(name="Cheddar Talk")  
     217    entry.blog = cheese_blog  
     218    entry.save()  
     219 
     220Updating a ``ManyToManyField`` works a little differently; use the ``add()`` 
     221method on the field to add a record to the relation:: 
     222 
    213223    joe = Author.objects.create(name="Joe") 
    214     entry.author = joe 
    215     entry.save() 
     224    entry.authors.add(joe) 
    216225 
    217 Django will complain if you try to assign an object of the wrong type. 
     226Django will complain if you try to assign or add an object of the wrong type. 
     227You can find out more about `related objects`_ fields below. 
    218228 
    219229How Django knows to UPDATE vs. INSERT 
    220230-------------------------------------