Ticket #907: 907.diff

File 907.diff, 2.0 KB (added by garthk, 18 years ago)

Documentation fix

  • docs/forms.txt

     
    340340      ``FormWrapper`` does not modify ``new_data`` in any way, and templates
    341341      cannot, so this is perfectly safe.
    342342
     343      If your object has a ``ManyToManyField``, you'll need to manually
     344      update ``new_data`` with details of the related objects. Let's say
     345      you're tagging your Places. In your model, that might looks like
     346      this::
     347
     348          from django.models.tags import tags
     349         
     350          class Place(meta.Model):
     351              # ... all the other fields
     352              tags = ManyToManyField(tags.Tag, blank=True)
     353
     354      Details of a ``ManyToManyField``, however, won't be present in the
     355      ``__dict__`` of your instance; they're stored in a separate table
     356      and fetched and manipulated with ``get_*_list``, ``get_*_count``, and
     357      ``set_*`` methods on your object (see the
     358      `Many-to-one relationship model example`_ for a full example). So,
     359      you have to add a little more code to make these details available
     360      to the ``ChangeManipulator``. Continuing our example from above,
     361      all we need is one extra line in ``edit_place``::
     362
     363          # This makes sure the form accurate represents the fields of the place.
     364          new_data = place.__dict__
     365          # ... and this handles the ManyToManyField for place's tags.
     366          new_data['tags'] = [t.id for t in place.get_tag_list()]
     367
    343368    * The above example uses a different template, so create and edit can be
    344369      "skinned" differently if needed, but the form chunk itself is completely
    345370      identical to the one in the create form above.
    346371
     372.. _Many-to-one relationship model example: http://www.djangoproject.com/documentation/models/many_to_one/
     373
    347374The astute programmer will notice the add and create functions are nearly
    348375identical and could in fact be collapsed into a single view. This is left as an
    349376exercise for said programmer.
Back to Top