Django

Code

Ticket #4214: db-api.patch

File db-api.patch, 0.7 kB (added by david@kazserve.org, 2 years ago)

patch that explains how to update a foreign key field

  • db-api.txt

    old new  
    134134 
    135135The ``save()`` method has no return value. 
    136136 
     137How to update a Foreign Key field 
     138--------------------------------- 
     139 
     140To update a foreign key field, assign a compatible database object to the field. 
     141 
     142Given a models.py contained this definition 
     143 
     144        class Blog(models.Model): 
     145                ... 
     146                owner = models.ForeignKey('Owner') 
     147 
     148This example would update ``Blog`` instance ``b5`` to point to "Jill" as the owner for the blog:: 
     149 
     150        p = Owner.objects.create(name="Jill") 
     151        b5.owner = p 
     152        b5.save() 
     153 
    137154How Django knows to UPDATE vs. INSERT 
    138155------------------------------------- 
    139156