7.3.4 Creating a Clean Method for the Tag
Model name
Field
Recall that in Chapter 3, we decided that our tag name
field should all be lowercase because of database alphabetization. There is, at this point, nothing stopping a user from submitting a tag name
in all caps. In the following subsection, we ensure that any string value submitted to our form will be submitted to the database in all lowercase.
In forms and models, field-specific clean methods come in the form of clean
_<fieldname
>()
. We can create a custom clean method in our TagForm
for the name
field by implementing the clean_name()
method. The goal of the clean method is to ensure that the value being passed is lowercase. In /organizer/forms.py
, we code as shown in Example 7.13.
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.