Ticket #17677: 17677.diff

File 17677.diff, 5.2 KB (added by Gary Reynolds, 13 years ago)
  • django/contrib/formtools/wizard/views.py

     
    385385            'initial': self.get_form_initial(step),
    386386        })
    387387        if issubclass(self.form_list[step], forms.ModelForm):
    388             # If the form is based on ModelForm, add instance if available.
    389             kwargs.update({'instance': self.get_form_instance(step)})
     388            # If the form is based on ModelForm, add instance if available
     389            # and not previously set.
     390            kwargs.setdefault('instance', self.get_form_instance(step))
    390391        elif issubclass(self.form_list[step], forms.models.BaseModelFormSet):
    391             # If the form is based on ModelFormSet, add queryset if available.
    392             kwargs.update({'queryset': self.get_form_instance(step)})
     392            # If the form is based on ModelFormSet, add queryset if available
     393            # and not previous set.
     394            kwargs.setdefault('queryset', self.get_form_instance(step))
    393395        return self.form_list[step](**kwargs)
    394396
    395397    def process_step(self, form):
  • django/contrib/formtools/tests/wizard/wizardtests/views.py

     
     1from django.contrib.auth.models import User
     2from django.contrib.formtools.wizard.views import CookieWizardView
     3
     4
     5class ModelWizard(CookieWizardView):
     6
     7    def get_form_kwargs(self, step):
     8        return {'instance': User.objects.get(is_staff=True)}
     9
     10
     11class QuerySetWizard(CookieWizardView):
     12
     13    def get_form_kwargs(self, step):
     14        return {'queryset': User.objects.filter(is_staff=True)}
  • django/contrib/formtools/tests/wizard/wizardtests/tests.py

     
    331331        response = view(factory.get('/'))
    332332        self.assertEquals(response.context_data['test_key'], 'test_value')
    333333        self.assertEquals(response.context_data['another_key'], 'another_value')
     334
     335
     336class WizardModelTests(TestCase):
     337
     338    def setUp(self):
     339        super(WizardModelTests, self).setUp()
     340        # Create two users so we can filter by is_staff when handing our
     341        # wizard a queryset keyword argument.
     342        User.objects.create(username='test1', email='test1@example.com')
     343        User.objects.create(username='test2', email='test2@example.com',
     344            is_staff=True)
     345
     346    def test_instance_is_maintained(self):
     347        from .forms import UserForm
     348        from .views import ModelWizard
     349
     350        factory = RequestFactory()
     351
     352        view = ModelWizard.as_view([UserForm])
     353        response = view(factory.get('/'))
     354
     355        instance = response.context_data['wizard']['form'].instance
     356
     357        self.assertNotEqual(None, instance.pk)
     358        self.assertEqual('test2@example.com', instance.email)
     359
     360    def test_queryset_is_maintained(self):
     361        from .forms import UserFormSet
     362        from .views import QuerySetWizard
     363
     364        factory = RequestFactory()
     365
     366        view = QuerySetWizard.as_view([UserFormSet])
     367        response = view(factory.get('/'))
     368
     369        formset = response.context_data['wizard']['form']
     370        queryset = formset.queryset
     371        emails = list(queryset.values_list('email', flat=True))
     372
     373        self.assertNotEqual(None, queryset)
     374        self.assertEqual(1, formset.total_form_count())
     375        self.assertEqual(['test2@example.com'], emails)
  • django/contrib/formtools/tests/wizard/wizardtests/forms.py

     
    22import tempfile
    33
    44from django import forms
     5from django.contrib.auth.models import User
    56from django.core.files.storage import FileSystemStorage
    67from django.forms.formsets import formset_factory
     8from django.forms.models import modelformset_factory
    79from django.http import HttpResponse
    810from django.template import Template, Context
    911
     
    5052            context.update({'another_var': True})
    5153        return context
    5254
     55class UserForm(forms.ModelForm):
     56    class Meta:
     57        model = User
     58        fields = ('username', 'email')
     59
     60UserFormSet = modelformset_factory(User, form=UserForm, extra=0)
     61
    5362class SessionContactWizard(ContactWizard):
    5463    storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage'
    5564
  • django/contrib/formtools/tests/wizard/__init__.py

     
    1515    CookieWizardTests,
    1616    WizardTestKwargs,
    1717    WizardTestGenericViewInterface,
     18    WizardModelTests,
    1819)
Back to Top