7.4.1 Creating PostForm

We use a ModelForm to connect to our Post tag in Example 7.26. We also make sure to lower the case on the slug field value via the field-specific clean method, remembering to return the value for proper processing.

Example 7.26: Project Code

blog/forms.py in afcd636772

 1  from django import forms  2  3  from .models import Post  4  5  6  class PostForm(forms.ModelForm):  7      class Meta:  8          model = Post  9          fields = '--all--' 10 11      def clean_slug(self): 12          return self .cleaned_data['slug'].lower()

Unlike what we do for our other forms, we don’t need to write the clean_slug() method to ensure that create is never submitted as the slug field value. Uniqueness ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.