saving to two models from one class view
I have been banging around trying to write to two models from one view for a couple days and just cannot figure it out.
[u'ManagementForm data is missing or has been tampered with']
-- I am using a CreateView. I want to write to a model with just the created_by user and a default value of False in app_complete to. I'm getting this error:
Could anyone help me figure out how to save to two models from a class view?
Models I'm trying to save to:
class Application(models.Model):
app_complete = models.BooleanField(default=False)
created_by = models.ForeignKey(User, unique=True)
class Applicant(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
title = models.CharField(max_length=100)
... more fields
My forms view:
class RegistrantForm(forms.ModelForm):
ADDRESS_CHOICES = (('home', 'Home',), ('business', 'Business',))
address_type = forms.ChoiceField(
choices=ADDRESS_CHOICES, widget=forms.Select
)
street2 = forms.CharField(required=False)
cell_phone = forms.CharField(required=False)
fax_phone = forms.CharField(required=False)
class Meta:
model = Applicant
fields = ('first_name', 'last_name', 'title', 'years_at_job', 'street1', 'street2', 'city', 'state', 'zip', 'address_type', 'residence', 'phone', 'cell_phone',
'fax_phone')
exclude = ['created_by'] # Will be taken from the request
class ApplicationForm(forms.ModelForm):
app_complete = forms.BooleanField(required=True, label="Check this to confirm the application is complete.")
class Meta:
model = Application
fields = ('app_complete', 'created_by')
ApplicationFormSet = inlineformset_factory(Application, Applicant)
Then my view which doesn't work:
class RegistrantCreate(CreateView):
model = Applicant
form_class = RegistrantForm
template_name = 'applicant_form.html'
success_url = '/requestform/update2/'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(RegistrantCreate, self).dispatch(*args, **kwargs)
def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
application_form = ApplicationFormSet()
return self.render_to_response(
self.get_context_data(form=form,
applicaton_form=application_form))
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
application_form = ApplicationFormSet(self.request.POST)
if (form.is_valid() and application_form.is_valid() and
application_form.is_valid()):
return self.form_valid(form, application_form)
else:
return self.form_invalid(form, application_form)
def form_valid(self, form, application_form):
"""
Called if all forms are valid. Creates a Recipe instance along with
associated Ingredients and Instructions and then redirects to a
success page.
"""
self.object = form.save(commit=False)
self.object.created_by = self.request.user
self.object = form.save()
application_form.instance = self.object
application_form.save(commit=False)
application_form.instance.created_by = self.request.user
application_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, application_form):
"""
Called if a form is invalid. Re-renders the context data with the
data-filled forms and errors.
"""
return self.render_to_response(
self.get_context_data(form=form,
application_form=application_form)
)
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4429a32-dfe1-42fd-a9ce-ccf8e7e2885b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home