#17464: [patch] Work with partial forms with the generic CreateView -------------------------------+-------------------- Reporter: madjar | Owner: nobody Type: New feature | Status: new Component: Generic views | Version: 1.3 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 1 Easy pickings: 0 | UI/UX: 0 -------------------------------+-------------------- 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. -- Ticket URL: <https://code.djangoproject.com/ticket/17464> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.