Accessing request.user in class based generic view CreateView in order to set FK field in Django

How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect. class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(PlaceFormView, self).dispatch(*args, **kwargs) def form_valid(self, form): obj = form.save(commit=False) obj.created_by = self.request.user obj.save() return http.HttpResponseRedirect(self.get_success_url())

Use get_queryset() method or set queryset variable?

In your example, overriding queryset and get_queryset have the same effect. I would slightly favour setting queryset because it’s less verbose. When you set queryset, the queryset is created only once, when you start your server. On the other hand, the get_queryset method is called for every request. That means that get_queryset is useful if … Read more

tech