update a field of a related table on form save

101 views
Skip to first unread message

Brad Rice

unread,
Jul 29, 2014, 9:26:21 PM7/29/14
to django...@googlegroups.com
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?
        return HttpResponseRedirect(reverse('requestform:app_check', kwargs={'app_id': obj.id}))

Any help would be appreciated.

ma...@tubeards.com

unread,
Jul 30, 2014, 2:04:42 AM7/30/14
to django...@googlegroups.com
Hi Brad,

i would register a 'post_save' signal (https://docs.djangoproject.com/en/dev/ref/signals/#post-save) on the first model and then, just hit the save method on the Application model, this will automaticly update the time.

Cheers,
Marc

Babatunde Akinyanmi

unread,
Jul 30, 2014, 2:33:57 AM7/30/14
to Django users

Hi Brad, my response is inline

#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...@googlegroups.com.
> To post to this group, send email to django...@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.

Brad Rice

unread,
Jul 30, 2014, 9:15:55 PM7/30/14
to django...@googlegroups.com
Thank you both for those suggestions. I got Tundebazby's to work as it was closest to what I was doing. I'll have to study the post_save some more.
Reply all
Reply to author
Forward
0 new messages