Shai CLI
Sign in
Sign up
.cursorrules
# Django Development Rules You are an expert in Python, Django, and Django REST Framework. ## Code Style - Follow PEP 8 and Django coding style - Use type hints where appropriate - Write docstrings for classes and functions - Keep views thin, models fat ## Django Patterns - Use class-based views (CBVs) for standard CRUD - Use function-based views (FBVs) for custom logic - Use Django's ORM effectively - Implement custom managers for complex queries ## Project Structure ``` project/ ├── config/ # Project settings │ ├── settings/ │ │ ├── base.py │ │ ├── development.py │ │ └── production.py │ ├── urls.py │ └── wsgi.py ├── apps/ │ └── users/ # App example │ ├── models.py │ ├── views.py │ ├── serializers.py │ ├── urls.py │ └── tests/ └── manage.py ``` ## Models - Use verbose_name for fields - Define __str__ for all models - Use choices for enumerated fields - Add indexes for frequently queried fields - Use signals sparingly ```python class User(AbstractUser): """Custom user model.""" class Role(models.TextChoices): ADMIN = "admin", "Administrator" USER = "user", "Regular User" role = models.CharField( max_length=20, choices=Role.choices, default=Role.USER, ) def __str__(self): return self.email ``` ## Django REST Framework - Use serializers for validation - Use viewsets with routers for standard CRUD - Implement proper permissions - Use pagination for list views - Version your API ## Testing - Use pytest-django - Write tests for models, views, and serializers - Use factories with factory_boy - Test edge cases and permissions ```python @pytest.mark.django_db class TestUserModel: def test_create_user(self, user_factory): user = user_factory() assert user.pk is not None ``` ## Security - Use Django's built-in protections - Validate all inputs - Use environment variables for secrets - Keep DEBUG=False in production - Implement rate limiting ## Performance - Use select_related and prefetch_related - Cache expensive queries - Use database indexes - Profile with Django Debug Toolbar
plaintext
Read only