7.3.6 Creating a Clean Method for the Tag
Model slug
Field
Our slug
field clean method, clean_slug()
, must do two things:
1. Ensure that slug
is lowercase (to prevent alphabetization and conflicts).
2. Ensure slug
is not create
(to prevent URL path collision).
Item 1 is straightforward, as we saw in the clean_name()
method. However, in the event that item 2 fails, there is no clear and simple course of action to rectify the problem. Following Python’s explicit nature, we should therefore raise an error. As such, we must first import the ValidationError
exception, which Django expects developers to raise in the event a value cannot be validated. In /organizer/forms.py
, we add the import from django.core.exceptions import ValidationError
. We can ...
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.