Re: Class Based CreateView with foreign key question
Actually that totally didn't work - I don't know what I was thinking. The form_valid method doesn't work for saving the model.
I have it working now but I guess the question I had is if using dispatch for setting variables in general is a good idea.
On Monday, June 10, 2013 3:38:09 PM UTC-7, Jason Arnst-Goodrich wrote:
I'm having trouble wrapping my head around how to use generic class based views properly when it comes to creating objects associated with already created objects.The concept here is adding File objects to a Case object.The url pattern would be something like:url(r'^case/(?P<pk>\d+)/add/$', AddFileToCase.as_view(), {}, 'file-add') So going to case/1/add would prompt the user with a form to add a file to case with pk = 1.The following code below is doing what I want but I'm not sure it's the best way to handle it. I feel the documentation to be very lacking in this area.class AddFileToCase(CreateView):model = Fileform_class = FileForm #all of the file fields exposed except for the foreign key relationship field to Casedef dispatch(self, request, *args, **kwargs):self.case = Case.objects.get(pk=self.kwargs['pk']) return super(AddFileToCase, self).dispatch(request, *args, **kwargs)def form_valid(self, form):form.instance.case= self.casereturn super(AddFileToCase, self).form_valid(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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home