Skip to content
Browse files

Merge pull request #3 from SystemParadox/cbv-docs

CBV form docs
  • Loading branch information...
2 parents 6775bbe + 2157dc4 commit c3e4a43d61e7f27d5a327a31e856c03dc7ffeb3e @pydanny committed Jun 7, 2012
Showing with 61 additions and 0 deletions.
  1. +61 −0 docs/topics/form-handling-class-based-views.txt
View
61 docs/topics/form-handling-class-based-views.txt
@@ -39,6 +39,8 @@ The view can be constructed using a FormView::
success_url = '/thanks/'
def form_valid(self, form):
+ # This method is called when valid form data has been POSTed.
+ # It should return an HttpResponse.
form.send_email()
return super(ContactView, self).form_valid(form)
@@ -58,12 +60,22 @@ they can work out which model class to use:
* If ``get_object`` returns an object, the class of that object will be used
* If a ``queryset`` is given, the model for that queryset will be used
+Model views provide a ``form_valid`` implementation that saves the model
+automatically.
+You can override/extend this method if you have any special requirements
+(see below for examples).
+
You don't even need to provide a success_url for ``CreateView`` or
``UpdateView`` - they will use ``model.get_absolute_url()`` if available.
If you want to use a custom ``ModelForm`` (to add extra constraints or validation)
simply set :attr:`form_class` to your custom form class.
+.. note::
+ When specifying a custom form class, you must still specify the model,
+ even though the form_class may be a ModelForm.
+ (See https://code.djangoproject.com/ticket/15125)
+
Example of full add/edit/delete for one model::
# models.py
@@ -87,6 +99,7 @@ Example of full add/edit/delete for one model::
class AuthorDelete(DeleteView):
model = Author
+ success_url = '/authors/'
# urls.py
from django.conf.urls import patterns, url
@@ -109,3 +122,51 @@ Example of full add/edit/delete for one model::
If you wish to have separate templates for ``CreateView`` and ``UpdateView``, you
can set either :attr:`template_name` or :attr:`template_name_suffix` on
your view class.
+
+Models and request.user
+-----------------------
+
+To track the user that created an object using a ``CreateView``, you can do the
+following...
+
+First, add the foreign key relation to the model::
+
+ # models.py
+ from django import models
+ from django.contrib.auth import User
+
+ class Author(models.Model):
+ name = models.CharField(max_length=200)
+ created_by = models.ForeignKey(User)
+
+ def get_absolute_url(self):
+ return '/author/%s/' % self.pk
+
+Create a custom ``ModelForm`` in order to exclude the ``created_by`` field and
+prevent the user from editing it:
+
+.. code-block:: python
+
+ # forms.py
+ from django import forms
+ from myapp.models import Author
+
+ class AuthorForm(forms.ModelForm):
+ class Meta:
+ model = Author
+ exclude = ('created_by',)
+
+In the view, use the custom ``form_class`` and override ``form_valid`` to add the user::
+
+ # views.py
+ from django.views.generic.edit import CreateView
+ from myapp.models import Author
+ from myapp.forms import AuthorForm
+
+ class AuthorCreate(CreateView):
+ form_class = AuthorForm
+ model = Author
+
+ def form_valid(self, form):
+ form.instance.created_by = self.request.user
+ return super(AuthorCreate, self).form_valid(form)

0 comments on commit c3e4a43

Please sign in to comment.
Something went wrong with that request. Please try again.