django 1.3 - Saving multiple forms does a partial save
I'm having some troubles making multiple forms in django. I have two models, Space
and Entity
. Each space can have N entities related to it.
I've created a form with two ModelForms, and writed a view that works "apparently". This is, it saves the space form, but not the data on the entity form. I does not make sense to me that saving the forms does a "partial" storage of it.
views.py
def all(items):
import operator
return reduce(operator.and_, [bool(item) for item in items])
def create_space(request):
"""
Create new space with its related entities. In this view the author
field is automatically filled.
"""
space_form = SpaceForm(request.POST or None, request.FILES or None)
entity_forms = [EntityForm(request.POST or None, prefix=str(x)) for x in range(0,3)]
if request.POST:
space_form_uncommited = space_form.save(commit=False)
space_form_uncommited.author = request.user
if space_form.is_valid() and all([ef.is_valid() for ef in
开发者_JAVA百科 entity_forms]):
new_space = space_form_uncommited.save()
for ef in entity_forms:
ef_uncommited = ef.save(commit=False)
ef_uncommited.space = new_space
ef_uncommited.save()
# We add the created spaces to the user allowed spaces
space = get_object_or_404(Space, name=space_form_uncommited.name)
request.user.profile.spaces.add(space)
return redirect('/spaces/' + space.url)
return render_to_response('spaces/space_add.html',
{'form': space_form,
'entityform_0': entity_forms[0],
'entityform_1': entity_forms[1],
'entityform_2': entity_forms[2]},
context_instance=RequestContext(request))
forms.py
class SpaceForm(ModelForm):
"""
"""
class Meta:
model = Space
class EntityForm(ModelForm):
"""
"""
class Meta:
model = Entity
The template code is pasted here because is too long.
You don't need to create objects, ModelForm will do it for you, so remove lines space = Space()
and entity = Entity()
and don't pass any instance
to forms.
And don't reinvent all
function, it's already Python built-in. :-)
SOLVED
There was some kind of problem using RAW forms. I converted EntityForm to a ModelFormSet and after that, the Entities got saved.
Fixed also the entity.space delaration, it was storing a NoneObject
.
Final code:
views.py
def create_space(request):
space_form = SpaceForm(request.POST or None, request.FILES or None)
entity_forms = EntityFormSet(request.POST or None, request.FILES or None,
queryset=Entity.objects.none())
if request.POST:
space_form_uncommited = space_form.save(commit=False)
space_form_uncommited.author = request.user
if space_form.is_valid() and entity_forms.is_valid():
new_space = space_form_uncommited.save()
space = get_object_or_404(Space, name=space_form_uncommited.name)
ef_uncommited = entity_forms.save(commit=False)
for ef in ef_uncommited:
ef.space = space
ef.save()
# We add the created spaces to the user allowed spaces
request.user.profile.spaces.add(space)
return redirect('/spaces/' + space.url)
return render_to_response('spaces/space_add.html',
{'form': space_form,
'entityformset': entity_forms},
context_instance=RequestContext(request))
forms.py
from django.forms.models import modelformset_factory
EntityFormSet = modelformset_factory(Entity, extra=3)
精彩评论