Skip to content

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

Closed
wants to merge 8 commits into from

2 participants

@timgraham timgraham commented on an outdated diff Oct 16, 2013
django/contrib/formtools/models.py
@@ -1 +1,21 @@
""" models.py (even empty) currently required by the runtests.py to enable unit tests """
+
+# coding: utf-8
@timgraham
Django member
timgraham added a note Oct 16, 2013

These models should go in the tests folder, otherwise they'll be installed in non-test situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 16, 2013
...o/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -405,3 +407,26 @@ 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()
+
+ def test_set_instance(self):
+ class InlineFormSetWizard(CookieWizardView):
+ instance = None
+
+ def get_form_instance(self, step):
+ if self.instance is None:
+ poet = Poet.objects.create(name='test')
@timgraham
Django member
timgraham added a note Oct 16, 2013

could we create these objects outside of the class? then we could reference them in the assertion using assertEqual rather than assertNotEqual(formset.instance, None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 16, 2013
django/contrib/formtools/wizard/views.py
@@ -406,9 +406,11 @@ 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) or \
@timgraham
Django member
timgraham added a note Oct 16, 2013

use parentheses in if statement so you can omit the backslash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 16, 2013
...o/contrib/formtools/tests/wizard/wizardtests/tests.py
+ instance = None
+
+ def get_form_instance(self, step):
+ if self.instance is None:
+ poet = Poet.objects.create(name='test')
+ poem = poet.poem_set.create(name='test poem')
+ self.instance = poet
+
+ return self.instance
+
+ view = InlineFormSetWizard.as_view([PoemFormSet])
+ response = view(self.rf.get('/'))
+ formset = response.context_data['wizard']['form']
+
+ self.assertNotEqual(formset.instance, None)
+
@timgraham
Django member
timgraham added a note Oct 16, 2013

no trailing newlines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 16, 2013
django/contrib/formtools/wizard/views.py
@@ -406,9 +406,11 @@ 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) or \
+ issubclass(form_class, forms.models.BaseInlineFormSet):
@timgraham
Django member
timgraham added a note Oct 16, 2013

issubclass allows a tuple for the 2nd argument: issubclass(form_class, (forms.ModelForm, forms.models.BaseInlineFormSet))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 17, 2013
django/contrib/formtools/models.py
@@ -1 +1,2 @@
""" models.py (even empty) currently required by the runtests.py to enable unit tests """
+
@timgraham
Django member
timgraham added a note Oct 17, 2013

please remove this change (and I think you forgot to add the new models.py file to the commit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 17, 2013
...o/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -10,7 +10,7 @@
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
@timgraham
Django member
timgraham added a note Oct 17, 2013

should be 2 newlines above class names as before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 17, 2013
...o/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -18,8 +18,10 @@ class Meta:
fields = '__all__'
+# Creating table formtools_testmodel
@timgraham
Django member
timgraham added a note Oct 17, 2013

remove comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on an outdated diff Oct 17, 2013
...o/contrib/formtools/tests/wizard/wizardtests/tests.py
+ 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)
+
@timgraham
Django member
timgraham added a note Oct 17, 2013

remove trailing newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Kathryn Kill... added some commits Oct 17, 2013
Kathryn Killebrew Move models.py; formatting. 57a23ca
Kathryn Killebrew Formatting 2f1699c
Kathryn Killebrew Formatting d4a7dfb
@flibbertigibbet

Okay, done.

@timgraham timgraham and 1 other commented on an outdated diff Oct 17, 2013
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:
@timgraham
Django member
timgraham added a note Oct 17, 2013

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

Okay, done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Kathryn Killebrew Formatting 9e894f5
@timgraham
Django member

Thanks for your persistence with all the edits here! One final note: you may want to configure your editor to remove trailing whitespace (difficult to see in GH review tool, you can run flake8 to find it though). I removed it and merged this in 7e5d7a7. Thanks again for your contribution!

@timgraham timgraham closed this Oct 17, 2013
@flibbertigibbet

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.