Django

Code

Changeset 7930

Show
Ignore:
Timestamp:
07/15/08 17:41:17 (4 months ago)
Author:
brosner
Message:

newforms-admin: Fixed #7230 -- Added a save_m2m method to BaseModelFormSet? when commit=False is passed to save. Thanks Books Travis for the original report.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/newforms/models.py

    r7854 r7930  
    332332        as necessary, and returns the list of instances. 
    333333        """ 
     334        if not commit: 
     335            self.saved_forms = [] 
     336            def save_m2m(): 
     337                for form in self.saved_forms: 
     338                    form.save_m2m() 
     339            self.save_m2m = save_m2m 
    334340        return self.save_existing_objects(commit) + self.save_new_objects(commit) 
    335341 
     
    354360                    self.changed_objects.append((obj, form.changed_data)) 
    355361                    saved_instances.append(self.save_existing(form, obj, commit=commit)) 
     362                    if not commit: 
     363                        self.saved_forms.append(form) 
    356364        return saved_instances 
    357365 
     
    366374                continue 
    367375            self.new_objects.append(self.save_new(form, commit=commit)) 
     376            if not commit: 
     377                self.saved_forms.append(form) 
    368378        return self.new_objects 
    369379 
  • django/branches/newforms-admin/docs/modelforms.txt

    r7730 r7930  
    455455 
    456456This gives you the ability to attach data to the instances before saving them 
    457 to the database. 
     457to the database. If your formset contains a ``ManyToManyField`` you will also 
     458need to make a call to ``formset.save_m2m()`` to ensure the many-to-many 
     459relationships are saved properly. 
    458460 
    459461Limiting the number of objects editable 
  • django/branches/newforms-admin/tests/modeltests/model_formsets/models.py

    r7613 r7930  
    1414        return self.title 
    1515 
     16class AuthorMeeting(models.Model): 
     17    name = models.CharField(max_length=100) 
     18    authors = models.ManyToManyField(Author) 
     19    created = models.DateField(editable=False) 
     20     
     21    def __unicode__(self): 
     22        return self.name 
     23 
    1624 
    1725__test__ = {'API_TESTS': """ 
     26 
     27>>> from datetime import date 
    1828 
    1929>>> from django.newforms.models import modelformset_factory 
     
    163173[<Author: Walt Whitman>] 
    164174 
     175Test the behavior of commit=False and save_m2m 
     176 
     177>>> meeting = AuthorMeeting.objects.create(created=date.today()) 
     178>>> meeting.authors = Author.objects.all() 
     179 
     180# create an Author instance to add to the meeting. 
     181>>> new_author = Author.objects.create(name=u'John Steinbeck') 
     182 
     183>>> AuthorMeetingFormSet = modelformset_factory(AuthorMeeting, extra=1, can_delete=True) 
     184>>> data = { 
     185...     'form-TOTAL_FORMS': '2', # the number of forms rendered 
     186...     'form-INITIAL_FORMS': '1', # the number of forms with initial data 
     187...     'form-MAX_FORMS': '0', # the max number of forms 
     188...     'form-0-id': '1', 
     189...     'form-0-name': '2nd Tuesday of the Week Meeting', 
     190...     'form-0-authors': [2, 1, 3, 4], 
     191...     'form-1-name': '', 
     192...     'form-1-authors': '', 
     193...     'form-1-DELETE': '', 
     194... } 
     195>>> formset = AuthorMeetingFormSet(data=data, queryset=AuthorMeeting.objects.all()) 
     196>>> formset.is_valid() 
     197True 
     198>>> instances = formset.save(commit=False) 
     199>>> for instance in instances: 
     200...     instance.created = date.today() 
     201...     instance.save() 
     202>>> formset.save_m2m() 
     203>>> instances[0].authors.all() 
     204[<Author: Charles Baudelaire>, <Author: Walt Whitman>, <Author: Paul Verlaine>, <Author: John Steinbeck>] 
     205 
     206# delete the author we created to allow later tests to continue working. 
     207>>> new_author.delete() 
     208 
    165209Test the behavior of max_num with model formsets. It should properly limit 
    166210the queryset to reduce the amount of objects being pulled in when not being