---
description: This rule explains Django conventions and best practices for backend development.
globs: **/*.py
alwaysApply: false
---
# Django rules
- Use `python manage.py startapp` to create new apps within your project
- Keep models in `models.py` and register them in `admin.py` for admin interface
- Use Django's ORM instead of raw SQL queries
- Avoid N+1 queries with `select_related` and `prefetch_related`:
```python
# Good pattern
users = User.objects.select_related('profile')
posts = Post.objects.prefetch_related('tags')
```
- Use Django forms for validation:
```python
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email']
```
- Create custom model managers for common queries:
```python
class ActiveUserManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)
```
- Use Django's built-in authentication system
- Store settings in environment variables and access via `settings.py`
This rule helps you follow Django best practices and conventions when developing backend applications. It provides guidance on structuring your Django projects, using the ORM effectively, and implementing common Django patterns to maintain clean, efficient code.
The Django rule serves as a reference guide for Django development within Cursor. It encapsulates common Django conventions and best practices to help you write more efficient and maintainable code. The rule covers several important aspects of Django development:
The rule emphasizes using Django's built-in commands for creating new apps within your project structure. This ensures your project follows Django's conventional layout, making it easier for other developers to understand and navigate your codebase.
One of the most valuable aspects of this rule is its guidance on database optimization. It recommends:
select_related
and prefetch_related
For example, instead of fetching related objects in separate queries:
# Fetches all users, then makes a separate query for each profile
users = User.objects.all()
for user in users:
profile = user.profile # Additional query for each user
The rule recommends this optimized approach:
# Fetches users and their profiles in a single query
users = User.objects.select_related('profile')
The rule encourages using Django's form system for data validation, which helps maintain data integrity while reducing the amount of code you need to write:
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email']
For frequently used queries, the rule suggests creating custom model managers:
class ActiveUserManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)
The Django rule (django.mdc
) is designed to be automatically applied when working with Python files in your Django project. This happens through the glob pattern **/*.py
, which means the rule will be activated whenever you're editing any Python file in your project.
To use this rule:
django.mdc
) is placed in your project's .cursor/rules
directoryYou can also manually invoke the rule by typing @django
in the Cmd-K prompt or in chat, which is useful when you want Django-specific assistance regardless of the current file type.
By following the practices in this rule, you'll:
This rule is particularly helpful for teams working on Django projects, as it promotes consistency and best practices across the codebase.