Tuesday, July 29, 2014

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

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

Am Mittwoch, 30. Juli 2014 03:26:21 UTC+2 schrieb Brad Rice:
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.

--
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/08d28a7e-7e95-4b3e-9117-b8573c2bba45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment