On Mon, Oct 13, 2014 at 3: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
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 -- 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/CAG_-i2%3DCNRpdeAG7T7AieYRY3943S9N4sOvb8G5qGzpTtecNzQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.