Problem when getting the ID of the record just created

231 views
Skip to first unread message

Daniel Grace

unread,
Oct 13, 2014, 9:13:45 AM10/13/14
to django...@googlegroups.com
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
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.

Joseph Mutumi

unread,
Oct 13, 2014, 9:24:17 AM10/13/14
to django-users@googlegroups com
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)

--
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/8aaceba9-0203-489a-a57f-09fa26ce163c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Giuseppe Saviano

unread,
Oct 13, 2014, 9:37:33 AM10/13/14
to django...@googlegroups.com
On Mon, Oct 13, 2014 at 3:13 PM, Daniel Grace <danw...@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


Your "flow" should be in self.object after you've called
super(CreateFlow, self).form_valid(form)

If I understand your problem, it should be:

def form_valid(self, form):
r = super(CreateFlow, self).form_valid(form)
log = Log.objetcs.create(user=request.user, flow=self.object, state=1)
return r

Also note that you don't need
User.objects.get(pk=self.request.user.id) because request.user is your
user object.

You can check here
http://ccbv.co.uk/projects/Django/1.7/django.views.generic.edit/CreateView/
and here
https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-editing/#createview


--
$ gpg --recv-key da5098a7

Giuseppe Saviano

unread,
Oct 13, 2014, 9:43:22 AM10/13/14
to django...@googlegroups.com
> User.objects.get(pk=self.request.user.id) because request.user is your
> user object.
>

it's self.request.user - sorry.
But the sense is the same: you don't need User.objects.get

Daniel Grace

unread,
Oct 13, 2014, 10:34:56 AM10/13/14
to django...@googlegroups.com
Thanks for the help.  I got the ID working and I used the "self.user.request".
Reply all
Reply to author
Forward
0 new messages