Skip to content
bogdans83 edited this page Mar 23, 2013 · 5 revisions

A checklist on how to build a Django Model

Prerequisities

Checklist

Define fields

  1. 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
  2. Have you checked all available field options and see if they apply?
    • Is the field an index?
    • Should the field have a default?
    • Is help_text set? It's useful in Admin and to ensure code readability
    • Is the field unique?
  3. Did you define class Meta options?
    • Is a instance unique based on certain fields values?
    • Should instances be ordered by default?
    • Should fields used to retrieve an instance be indexes?

Define field validations

  1. Does the field accept null or blank values?
  2. Can we use Django's default validators? to enforce field's constraints?
  3. 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

Define field validations

  1. Assuming you have fields since and until defined, since should be less than until

Define relationships validations

  1. Does the relationship has to exist? Ex: You should not insert an Assignment with a subject that does not exist
  2. Does the relationship has to be valid in context with any of the model's fields? (use clean_*)