commit 483ef288c31810eb0c061aa1bef25ee92d92b5b8
Author: Jakub Vysoky <jakub@borka.cz>
Date: Tue Jul 15 13:41:39 2008 +0200
Passing the unsaved created object to inline formsets for validation.
http://code.djangoproject.com/ticket/5780
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index 07f29ac..8e7a8eb 100644
a
|
b
|
class ModelAdmin(BaseModelAdmin):
|
506 | 506 | obj = self.model() |
507 | 507 | if request.method == 'POST': |
508 | 508 | form = ModelForm(request.POST, request.FILES) |
| 509 | if form.is_valid(): |
| 510 | obj = form.save(commit=False) |
| 511 | else: |
| 512 | obj = None |
509 | 513 | for FormSet in self.get_formsets(request): |
510 | 514 | inline_formset = FormSet(data=request.POST, files=request.FILES, |
511 | 515 | instance=obj, save_as_new=request.POST.has_key("_saveasnew")) |
512 | 516 | inline_formsets.append(inline_formset) |
513 | | if all_valid(inline_formsets) and form.is_valid(): |
| 517 | if all_valid(inline_formsets) and obj: |
514 | 518 | return self.save_add(request, form, inline_formsets, '../%s/') |
515 | 519 | else: |
516 | 520 | form = ModelForm(initial=dict(request.GET.items())) |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
569 | 573 | inline_formsets = [] |
570 | 574 | if request.method == 'POST': |
571 | 575 | form = ModelForm(request.POST, request.FILES, instance=obj) |
| 576 | if form.is_valid(): |
| 577 | new_obj = form.save( commit=False ) |
| 578 | else: |
| 579 | new_obj = obj |
572 | 580 | for FormSet in self.get_formsets(request, obj): |
573 | | inline_formset = FormSet(request.POST, request.FILES, instance=obj) |
| 581 | inline_formset = FormSet(data=request.POST, files=request.FILES, instance=new_obj) |
574 | 582 | inline_formsets.append(inline_formset) |
575 | 583 | |
576 | 584 | if all_valid(inline_formsets) and form.is_valid(): |