Cursor Rules for
Django

This rule explains Django conventions and best practices for backend development.
Back to rules
Type
Backend
Language(s)
Python
Stats
345 views
19 copies
11 downloads
django.mdc
---
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.

What this rule does

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:

  • Project organization and app creation
  • Model definition and admin registration
  • Database query optimization
  • Form handling and validation
  • Custom model managers
  • Authentication and settings management

Key Django practices

Project structure and organization

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.

Database efficiency

One of the most valuable aspects of this rule is its guidance on database optimization. It recommends:

  • Using Django's ORM instead of writing raw SQL
  • Avoiding N+1 query problems with 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')

Forms and validation

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']

Custom model managers

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)

Using Django in Cursor

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:

  1. Ensure the rule file (django.mdc) is placed in your project's .cursor/rules directory
  2. Open any Python file in your Django project
  3. The rule will automatically provide context to Cursor's AI when you're working on these files

You 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.

Benefits of using this rule

By following the practices in this rule, you'll:

  • Write more maintainable Django code
  • Avoid common performance pitfalls
  • Structure your project according to Django conventions
  • Leverage Django's built-in features effectively

This rule is particularly helpful for teams working on Django projects, as it promotes consistency and best practices across the codebase.

Tips for Django development in Cursor

  • When asking Cursor's AI for help with Django-related tasks, reference this rule for more accurate, convention-following responses
  • Use this rule as a quick reference when you forget specific Django patterns
  • For large Django projects, consider expanding this rule with project-specific conventions
Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later