Tuesday, July 29, 2014

update a field of a related table on form save

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'

    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
        a = Application.objects.get(created_by=self.request.user)
       ## how do I save to the Application here?
        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.

No comments:

Post a Comment