Linking Two Models in a Multi-Model Form

Multiple Models in a single django ModelForm?

You can just show both forms in the template inside of one <form> html element. Then just process the forms separately in the view. You'll still be able to use form.save() and not have to process db loading and saving yourself.

In this case you shouldn't need it, but if you're going to be using forms with the same field names, look into the prefix kwarg for django forms. (I answered a question about it here).

Single CreateView in django for submitting multiple ModelForm data to multiple Model

override get function, because get request can not get work_form in default

def get(self, request, *args, **kwargs):
form = self.form_class(**self.get_form_kwargs())
work_form = self.work_form_class(prefix='work_form')
return render(request, self.template_name, {'form': form, 'work_form': work_form})

Django Forms, having multiple Models in Meta class?

No. But you don't need to. Instead of instantiating and validating a single form, do it for each type of form you need to support.

# Define your model forms like you normally would
class StudentForm(ModelForm):
...

class TutorForm(ModelForm):
...

class RegistrationForm(Form):
email = ...
...

# Your (simplified) view:
...
context = {
'student_form': StudentForm(),
'tutor_form': TutorForm(),
'registration_form': RegistrationForm()
}
return render(request, 'app/registration.html', context)

# Your template
...
<form action="." method="post">
{{ student_form }}
{{ tutor_form }}
{{ registration_form }}
<input type="submit" value="Register">
</form>

If this means field names are duplicated across forms, use form prefixes to sort that out.

Model Binding on Multiple Model Form Submission from Strongly-Typed View

Blind guess:

change:

<%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%>

with:

<%= Html.TextBox("Complaint.Complainants[" + i + "].Surname",  
complainant.Surname)%>

Respectively - add "Complaint." before "Complainants[..."

EDIT:

This is NOT a right answer. Left it undeleted just because that might add some value until proper answer pops up.

EDIT2:

I might be wrong, but for me it seems there's problem with entity framework (or - with the way you use it). I mean - asp.net mvc manages to read values from request but can't initialize complainants collection.

Here it's written:

The InitializeRelatedCollection(TTargetEntity) method initializes an existing EntityCollection(TEntity) that was created by using the default constructor. The EntityCollection(TEntity) is initialized by using the provided relationship and target role names.

The InitializeRelatedCollection(TTargetEntity) method is used during deserialization only.

Some more info:

Exception:

  • InvalidOperationException

Conditions:

  • When the provided EntityCollection(TEntity) is already initialized.
  • When the relationship manager is already attached to an ObjectContext.
  • When the relationship manager already contains a relationship with this name and target role.

Somewhy InitializeRelatedCollection gets fired twice. Unluckily - i got no bright ideas why exactly. Maybe this little investigation will help for someone else - more experienced with EF. :)

EDIT3:

This isn't a solution for this particular problem, more like a workaround, a proper way to handle model part of mvc.

Create a viewmodel for presentation purposes only. Create a new domain model from pure POCOs too (because EF will support them in next version only). Use AutoMapper to map EFDataContext<=>Model<=>ViewModel.

That would take some effort, but that's how it should be handled. This approach removes presentation responsibility from your model, cleans your domain model (removes EF stuff from your model) and would solve your problem with binding.

Django Identifying Class Based View and Form Multiple Models

I'd do something like this (with betterforms):

class UserCreationMultiForm(MultiModelForm):
form_classes = {
'basic': UserBasicForm,
'address': UserAddressForm,
}

class UserDetailsView(View):
template = "mainapp/profile.html"
form_class = UserCreationMultiForm
success_url = reverse_lazy('home')

def form_valid(self, form):
# Save the user first, because the profile needs a user before it
# can be saved.
user = form['basic'].save()
address = form['address'].save(commit=False)
address.user = user
address.save()
return redirect(self.get_success_url())

Then rename your forms in your template to form.basic and form.address



Related Topics



Leave a reply



Submit