Best practices for Django Model Best Practices
.cursorrules in your project root# Django Model Best Practices
=====================================
## Core Principles
-------------------
1. **Separation of Concerns**: Keep models focused on data representation and avoid mixing business logic.
2. **Readability**: Use clear and concise naming conventions, and follow PEP 8 guidelines.
3. **Reusability**: Design models to be reusable across the application.
## Code Style Guidelines
------------------------
### Naming Conventions
* **Model Names**: Use singular nouns (e.g., `Book`, not `Books`).
* **Field Names**: Use lowercase with words separated by underscores (e.g., `first_name`).
* **Related Field Names**: Use the related model name followed by `_id` (e.g., `author_id`).
### Typing
* **Use Type Hints**: Include type hints for model fields and methods.
* **Field Types**: Use Django's built-in field types (e.g., `models.CharField`, `models.IntegerField`).
### Model Structure
* **Use `Meta` Class**: Define a `Meta` class to specify model metadata (e.g., `verbose_name`, `ordering`).
* **Field Ordering**: Define fields in the following order:
1. Primary key field (`id`)
2. Foreign key fields
3. Other fields
## Best Practices
------------------
### Model Definition
* **Use `models.Model` as Base Class**: Inherit from `models.Model` for all models.
* **Define `__str__` Method**: Implement a `__str__` method to provide a human-readable representation of the model instance.
* **Use `verbose_name` and `verbose_name_plural`**: Specify verbose names for models and their plural forms.
### Field Definitions
* **Use `null` and `blank` Correctly**: Set `null=True` for fields that can be null in the database, and `blank=True` for fields that can be blank in forms.
* **Specify Field Lengths**: Define lengths for character fields (e.g., `models.CharField(max_length=255)`).
### Relationships
* **Use `ForeignKey` for Many-to-One Relationships**: Establish many-to-one relationships using `ForeignKey`.
* **Use `ManyToManyField` for Many-to-Many Relationships**: Establish many-to-many relationships using `ManyToManyField`.
## Common Pitfalls to Avoid
---------------------------
* **Avoid Using `null=True` for Foreign Keys**: Instead, use `blank=True` and set a default value or use a `ForeignKey` with `on_delete=models.SET_NULL`.
* **Don't Override `save` Method**: Avoid overriding the `save` method; instead, use signals or custom methods to perform additional logic.
* **Don't Use `select_related` and `prefetch_related` Excessively**: Use these methods judiciously to avoid unnecessary database queries.
Example of a well-structured Django model:
```python
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey('Author', on_delete=models.CASCADE)
publication_date = models.DateField()
class Meta:
verbose_name = 'book'
verbose_name_plural = 'books'
ordering = ['title']
def __str__(self):
return self.title
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Django Model Best Practices.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.