Should get the flow instance from the form.save() so:

user = User.objects.get(pk=self.request.user.id)
flow = form.save()
log = Log(user=user, flow=flow, state=1)

On Mon, Oct 13, 2014 at 4:13 PM, Daniel Grace <danwgr...@gmail.com> wrote:

> Hi,
> I have problem when getting the ID of the record just created.
>
> class CreateFlow(CreateView):
> model = Flow
> fields = ['state']
> template_name = 'create_flow.html'
> def form_valid(self, form):
> user = User.objects.get(pk=self.request.user.id)
> flow = Flow.objects.get(pk=self.kwargs['pk'])
> log = Log(user=user, flow=flow, state=1)
> log.save()
> return super(CreateFlow, self).form_valid(form)
>
> ... gives the error:
> Request Method: POST
> Request URL: http://127.0.0.1:8000/create/
> Django Version: 1.7
> Exception Type: KeyError
> Exception Value:
> 'pk'
> Exception Location: C:\landy\cresta\flow\views.py in form_valid, line 64
> Python Executable: C:\landy\Scripts\python.exe
> Python Version: 3.4.0
> Python Path:
> ['C:\\landy\\cresta',
>  'C:\\Windows\\system32\\python34.zip',
>  'C:\\Python34\\DLLs',
>  'C:\\Python34\\lib',
>  'C:\\Python34',
>  'C:\\landy',
>  'C:\\landy\\lib\\site-packages']
> Server time: Mon, 13 Oct 2014 13:40:49 +0100
>
> What is the correct way to get the ID of the record just created?
>
> Thanks.
>
> --
> 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+unsubscr...@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/8aaceba9-0203-489a-a57f-09fa26ce163c%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/8aaceba9-0203-489a-a57f-09fa26ce163c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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+unsubscr...@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/CAN5idp__uazs18i6ms6Hmks%3DaecRR1B%3DcoH-h9yweVt%3D1ABADQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to