Antipatterns
- A GET request with side-effects
- A model with a …Modelsuffix
- calling all()beforecount()orfilter()etc.
- Chaining querysets together
- Checking if an object is created with instance.pk
- Checking ownership through the UserPassesTestMixin
- Checking request method with if request.POST
- Constructing a new form when validation fails
- Data duplication
- Fetching the logged in user with a query
- Fill the primary key gaps
- Filter on arbitrary input like request.GET
- Filtering in the template
- Foreign key with _idsuffix
- Giving related_name=…the same name as the relation
- Imports
- Manually constructing a slug
- Modifying slugs and primary keys of model objects
- non-atomic JSONFields
- (Over)use of .values()
- passing function references to reverse
- Passing parameters directly in the query string of a URL
- Plural model class names
- Processing request data manually
- Refer to the Usermodel directly
- Rendering content after a successful POST request
- Rendering into JavaScript
- Return a JsonResponsewithsafe=False
- Run makemigrationsin production
- Signals
- Use datetime.nowasdefault=…for acreated_onfield
- Use .get(…)to retrieve the object in a view
- Users controlling a primary key
- Using a FloatFieldfor currencies
- Using commit=Falsewhen altering the instance in aModelForm
- Using len(…)on aQuerySetwith no further use
- Using multiple forms on the same page without prefixing
- Using regular HTML comments instead of Django template comments
- Using request.POST or None
Patterns
- A default record per entity
- A field that is derived from another field
- A SET(…)delete handler with the object as parameter
- Annotate a condition as BooleanField
- Date(- Time)- Fields that store a week or month
- Dictionary lookups for the database
- Match multiple strings in a case-insensitive manner
- querying in the opposite direction
- Set values to a created/updated object in a class-based view