Re: saving to two models from one class view
Hi Brad,
-- At the moment, two models do not have any relationship. I think you made an error in Line 3:
created_by = models.ForeignKey(User, unique=True)
I think you meant "Applicant" instead of User.
Regards,
Arun
On Friday, January 10, 2014 3:13:42 AM UTC+5:30, Brad Rice wrote:
On Friday, January 10, 2014 3:13:42 AM UTC+5:30, Brad Rice wrote:
I have been banging around trying to write to two models from one view for a couple days and just cannot figure it out.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:[u'ManagementForm data is missing or has been tampered with']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 fieldsMy 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 = Applicantfields = ('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 requestclass ApplicationForm(forms.ModelForm): app_complete = forms.BooleanField(required=True, label="Check this to confirm the application is complete.") class Meta:model = Applicationfields = ('app_complete', 'created_by')ApplicationFormSet = inlineformset_factory(Application, Applicant) Then my view which doesn't work:class RegistrantCreate(CreateView):model = Applicantform_class = RegistrantFormtemplate_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 formand its inline formsets."""self.object = Noneform_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 inlineformsets with the passed POST variables and then checking them forvalidity."""self.object = Noneform_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() andapplication_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 withassociated Ingredients and Instructions and then redirects to asuccess page."""self.object = form.save(commit=False)self.object.created_by = self.request.userself.object = form.save()application_form.instance = self.objectapplication_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 thedata-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/c902a580-4ebb-41fd-a770-97ceb9c0e8d8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home