﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17464	Ease specifying non-form model attributes with the generic CreateView	Georges Dubus	Georges Dubus	"Here is a scenario to explain the problem :

In a multi-user blog application, when a post is created, its author field is set to the current user. We have a generic view like this :

{{{
class PostCreateView(CreateView):
    model = Post
    form_class = modelform_factory(Post, fields=('content',))
}}}

No valid object can be created with this view, because the objects are always missing the `author` field.

Currently, I could solve this problem by defining this method : 
{{{
    def get_form_kwargs(self):
        kwargs = super(PostCreateView, self).get_form_kwargs()
        kwargs.update({'instance': Post(author=self.request.user)})
        return kwargs
}}}

I have the feeling this is kind of hackish for a use case that seems common.

I've attached a patch that enable us to write this :
{{{
    def get_default_instance(self):
        return Post(author=self.request.user)
}}}

There is a test in the patch. Please review it, and if you find the change useful, I will also add the documentation."	New feature	closed	Generic views	1.3	Normal	wontfix		Georges Dubus florian+django@…	Accepted	1	0	0	1	0	0
