Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Save form with commit=True, not an object #11

Merged
merged 3 commits into from

2 participants

@szuliq

It is required, if You need to perform action on form save (e.g. have additional fields on form) when commit == True.

@AndrewIngram AndrewIngram merged commit 536be8f into AndrewIngram:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 13, 2012
  1. Save form with commit=True, not an object

    Krzysiek Szularz authored
  2. Test form is saved with commit=True

    Krzysiek Szularz authored
  3. Let update view actually use OrderForm

    Krzysiek Szularz authored
This page is out of date. Refresh to see the latest.
View
2  extra_views/advanced.py
@@ -16,7 +16,7 @@ def __init__(self, parent_model, request, instance):
class ModelFormWithInlinesMixin(ModelFormMixin):
def forms_valid(self, form, inlines):
- self.object.save()
+ self.object = form.save()
form.save_m2m()
for formset in inlines:
formset.save()
View
9 extra_views/tests/forms.py
@@ -6,6 +6,15 @@ class OrderForm(forms.ModelForm):
class Meta:
model = Order
+ def save(self, commit=True):
+ instance = super(OrderForm, self).save(commit=commit)
+
+ if commit:
+ instance.action_on_save = True
+ instance.save()
+
+ return instance
+
class ItemForm(forms.ModelForm):
flag = forms.BooleanField(initial=True)
View
1  extra_views/tests/models.py
@@ -14,6 +14,7 @@ class Order(models.Model):
name = models.CharField(max_length=255)
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
+ action_on_save = models.BooleanField(default=False)
class Item(models.Model):
View
20 extra_views/tests/tests.py
@@ -346,6 +346,26 @@ def test_update(self):
self.assertEquals(3, order.item_set.count())
self.assertEquals(2, Tag.objects.count())
self.assertEquals('Bubble Bath', order.item_set.all()[0].name)
+
+ def test_parent_instance_saved_in_form_save(self):
+ order = Order(name='Dummy Order')
+ order.save()
+
+ data = {
+ 'name': u'Dummy Order',
+ 'item_set-TOTAL_FORMS': u'0',
+ 'item_set-INITIAL_FORMS': u'0',
+ 'item_set-MAX_NUM_FORMS': u'',
+ 'tests-tag-content_type-object_id-TOTAL_FORMS': u'0',
+ 'tests-tag-content_type-object_id-INITIAL_FORMS': u'0',
+ 'tests-tag-content_type-object_id-MAX_NUM_FORMS': u'',
+ }
+
+ res = self.client.post('/inlines/1/', data, follow=True)
+ self.assertEqual(res.status_code, 200)
+
+ order = Order.objects.get(id=1)
+ self.assertTrue(order.action_on_save)
class CalendarViewTests(TestCase):
urls = 'extra_views.tests.urls'
View
3  extra_views/tests/views.py
@@ -1,7 +1,7 @@
from extra_views import FormSetView, ModelFormSetView, InlineFormSetView, InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView, CalendarMonthArchiveView
from extra_views.generic import GenericInlineFormSet, GenericInlineFormSetView
-from forms import AddressForm, ItemForm
+from forms import AddressForm, ItemForm, OrderForm
from formsets import BaseArticleFormSet
from models import Item, Order, Tag, Event
@@ -54,6 +54,7 @@ def get_success_url(self):
class OrderUpdateView(UpdateWithInlinesView):
model = Order
+ form_class = OrderForm
inlines = [ItemsInline, TagsInline]
template_name = 'extra_views/order_and_items.html'
Something went wrong with that request. Please try again.