Tuesday, July 29, 2014

Re: update a field of a related table on form save

Hi Brad, my response is inline

On 30 Jul 2014 02:26, "Brad Rice" <bradrice1@gmail.com> wrote:
>
> I have three tables. The first one is application which has a flag for if an app has been submitted as well as created date and modified date. The other two tables relate to the application table. I have two forms that get update those tables. What I want to do is update the application modified date from the save on the other forms.
>
> I have this in the Application model
> modified    = models.DateTimeField(auto_now_add=True)
>
> but since it is the other tables I am saving to, I'm not sure how to update that field whenever the other two tables update.
>
> My view for one of the forms looks like this:
>
> class AnswersUpdate(UpdateView):
>     model = Answers
>     form_class = AnswersForm
>     slug_field = 'created_by'
>     template_name = 'requestform/applicant_form.html'
>
>     @method_decorator(login_required(login_url='/accounts/login/'))
>     def dispatch(self, *args, **kwargs):
>         return super(AnswersUpdate, self).dispatch(*args, **kwargs)
>
>     def get_object(self, queryset=None):
>         return Answers.objects.get(created_by=self.request.user)
>
>     def form_valid(self, form):
>         obj = form.save(commit=False)
>         obj.created_by = self.request.user
>         obj.save()
>         a = Application.objects.get(created_by=self.request.user)
>        ## how do I save to the Application here?

#if its a new record, something like ....
application =  Application.objects.create(...)

#if you are updating a record, something like...
application = Application.objects.get(...) # can throw exception
application.field_name = something
application.save ()

>         return HttpResponseRedirect(reverse('requestform:app_check', kwargs={'app_id': obj.id}))
>
> Any help would be appreciated.
>
> --
> 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/2a2b6f24-b4c2-4575-9a52-85a892256c79%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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/CA%2BWjgXPpbqjetNYMSsa61q-HPy9M9dvMQ-P7NES6J6w-qtCS8w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment