Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set instance (not queryset) for InlineFormSet in wizard. #1726

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions django/contrib/formtools/tests/models.py
@@ -0,0 +1,24 @@
# coding: utf-8
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Poet(models.Model):
class Meta:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#model-style for the ordering of fields and Meta class.

also two spaces above class names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done.

app_label = 'formtools'

name = models.CharField(max_length=100)

def __str__(self):
return self.name

@python_2_unicode_compatible
class Poem(models.Model):
class Meta:
app_label = 'formtools'

poet = models.ForeignKey(Poet)
name = models.CharField(max_length=100)

def __str__(self):
return self.name
1 change: 1 addition & 0 deletions django/contrib/formtools/tests/wizard/__init__.py
Expand Up @@ -16,4 +16,5 @@
WizardTestKwargs,
WizardTestGenericViewInterface,
WizardFormKwargsOverrideTests,
WizardInlineFormSetTests,
)
24 changes: 23 additions & 1 deletion django/contrib/formtools/tests/wizard/wizardtests/tests.py
Expand Up @@ -10,15 +10,16 @@
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.formtools.wizard.views import CookieWizardView
from django.utils._os import upath
from django.contrib.formtools.tests.models import Poet, Poem

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be 2 newlines above class names as before


class UserForm(forms.ModelForm):
class Meta:
model = User
fields = '__all__'


UserFormSet = forms.models.modelformset_factory(User, form=UserForm, extra=2)
PoemFormSet = forms.models.inlineformset_factory(Poet, Poem, fields="__all__")


class WizardTests(object):
Expand Down Expand Up @@ -405,3 +406,24 @@ def get_form_kwargs(self, step):
self.assertEqual(formset.initial_form_count(), 1)
self.assertEqual(['staff@example.com'],
list(formset.queryset.values_list('email', flat=True)))


class WizardInlineFormSetTests(TestCase):
def setUp(self):
self.rf = RequestFactory()
self.poet = Poet.objects.create(name='test')
self.poem = self.poet.poem_set.create(name='test poem')

def test_set_instance(self):
poet = self.poet
class InlineFormSetWizard(CookieWizardView):
instance = None
def get_form_instance(self, step):
if self.instance is None:
self.instance = poet
return self.instance

view = InlineFormSetWizard.as_view([PoemFormSet])
response = view(self.rf.get('/'))
formset = response.context_data['wizard']['form']
self.assertEqual(formset.instance, self.poet)
7 changes: 4 additions & 3 deletions django/contrib/formtools/wizard/views.py
Expand Up @@ -406,9 +406,10 @@ def get_form(self, step=None, data=None, files=None):
'prefix': self.get_form_prefix(step, self.form_list[step]),
'initial': self.get_form_initial(step),
})
if issubclass(self.form_list[step], forms.ModelForm):
# If the form is based on ModelForm, add instance if available
# and not previously set.
form_class = self.form_list[step]
if issubclass(form_class, (forms.ModelForm, forms.models.BaseInlineFormSet)):
# If the form is based on ModelForm or InlineFormSet,
# add instance if available and not previously set.
kwargs.setdefault('instance', self.get_form_instance(step))
elif issubclass(self.form_list[step], forms.models.BaseModelFormSet):
# If the form is based on ModelFormSet, add queryset if available
Expand Down