Hi, I am using a CreateView. I want to create some related data on a table ("Log" model), so I need the ID of the record being created for the related field (see "flow" below). Also I need the ID of the user creating the record. Something like the following:
from django.utils import timezone
from django.views.generic.edit import CreateView
from django.contrib.auth.decorators import login_required
from flow.models import Flow, Log
class CreateFlow(CreateView):
model = Flow
fields = ['state']
template_name = 'create_flow.html'
# there is a problem with this:
def form_valid(self, form):
form.instance.created_by = self.request.user
# ... and this:
log.save()
return super(CreateFlow, self).form_valid(form)