-
Notifications
You must be signed in to change notification settings - Fork 2
Models
bogdans83 edited this page Mar 23, 2013
·
5 revisions
A checklist on how to build a Django Model
- Introduction to models
- Creating model instances
- Does the field has the appropriate data type? Always verify available field data types (and be up to date with these types or exotic type such as URLField)
- Is the field a relationship? How does the field relate to a separate model from the app's ecosystem? Read at least once available relationships
- Have you checked all available field options and see if they apply?
- Did you define class Meta options?
- Does the field accept null or blank values?
- Can we use Django's default validators? to enforce field's constraints?
- Does the field has extra validations pertaining only for this field (imagine you only have that field setup, other fields don't exist) such as the value should be even? Read how to write a validator
- Assuming you have fields since and until defined, since should be less than until
- Define such validations in the model's clean method https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.clean
- Split various validations in _clean_since_until methods which raise ValidationError if rules are not matched
- Does the relationship has to exist? Ex: You should not insert an Assignment with a subject that does not exist
- Does the relationship has to be valid in context with any of the model's fields? (use clean_*)