-
Notifications
You must be signed in to change notification settings - Fork 2
Forms
bogdans83 edited this page Mar 26, 2013
·
2 revisions
A checklist on how to use a Django Form. It's purpose (as we use it) is to sanitize and coerce input data received from a user to a consistent format based on which we can perform custom actions (overwritting the save() method to send a registration email) or create or update model instances (ModelForm use case).
- Introduction to forms (you can skip anything pertaining to form templates)
- Creating model instances based on model forms (pay close attention to the save, is_valid and errors section)
- Form validations (understand cleaning a specific attribute and validating fields that depend on each other)
- Model full_clean method
- Does the field has the appropriate data type? Always verify available form field's data types
- Have you checked all available form field options and see if they apply?
- Is the field required?
- Did you define class Meta options?
- Should we not update any of the fields of a form (eg for a ModelForm) from user data? Ex: Let A be a Model with created_at set to auto_add_now. We should exclude the field from being set by user data using Meta.exclude (or using Meta.fields), otherwise the user might set a date in the past
- Can we use default field validators?
- Use clean_fieldname to provide custom validation yielding a ValidationError in a false case
- Use clean_a_field_and_b_field to clean dependent fields and overwrite Form.clean() to call upon this method
- What could a user do (send as values) to screw us over? This is just a thought to always keep in mind when working with user data!
- Overwrite save() method to perform actions using sanitized data provided in self.cleaned_data or to update the form's instance object
TODO: Use case for collapsing form field values into an instance's value
- A field's initial option isn't equivalent to setting the default value of the field