Skills Plugins MCP Prompt Model 博客 我的中心
Development #design #api

django-patterns

Django architecture patterns, REST API design with DRF, ORM best practices, caching, signals, middleware, and production-grade Django apps. Use when building or reviewing Django apps, DRF APIs, ORM queries, or caching.

DeepseekModel Curated skill Quality Excellent · 90 v1.0.0

Get

https://deepseekmodel.com/api/download.php?id=affaan-m-ecc-skills-django-patterns-skill-md&format=skill
Download .skill Standard format with system_prompt and model_config, ready for any agent framework
The actual content of the system_prompt field in the .skill file.
name django-patterns description Django architecture patterns, REST API design with DRF, ORM best practices, caching, signals, middleware, and production-grade Django apps. Use when building or reviewing Django apps, DRF APIs, ORM queries, or caching. metadata {"origin":"ECC"} Django Development Patterns Production-grade Django architecture patterns for scalable, maintainable applications. When to Activate Building Django web applications Designing Django REST Framework APIs Working with Django ORM and models Setting up Django project structure Implementing caching, signals, middleware Project Structure Recommended Layout myproject/ ├── config/ │ ├── __init__.py │ ├── settings/ │ │ ├── __init__.py │ │ ├── base.py # Base settings │ │ ├── development.py # Dev settings │ │ ├── production.py # Production settings │ │ └── test.py # Test settings │ ├── urls.py │ ├── wsgi.py │ └── asgi.py ├── manage.py └── apps/ ├── __init__.py ├── users/ │ ├── __init__.py │ ├── models.py │ ├── views.py │ ├── serializers.py │ ├── urls.py │ ├── permissions.py │ ├── filters.py │ ├── services.py │ └── tests/ └── products/ └── ... Split Settings Pattern # config/settings/base.py from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent.parent SECRET_KEY = env( 'DJANGO_SECRET_KEY' ) DEBUG = False ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , 'rest_framework' , 'rest_framework.authtoken' , 'corsheaders' , # Local apps 'apps.users' , 'apps.products' , ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware' , 'whitenoise.middleware.WhiteNoiseMiddleware' , 'django.contrib.sessions.middleware.SessionMiddleware' , 'corsheaders.middleware.CorsMiddleware' , 'django.middleware.common.CommonMiddleware' , 'django.middleware.csrf.CsrfViewMiddleware' , 'django.contrib.auth.middleware.AuthenticationMiddleware' , 'django.contrib.messages.middleware.MessageMiddleware' , 'django.middleware.clickjacking.XFrameOptionsMiddleware' , ] ROOT_URLCONF = 'config.urls' WSGI_APPLICATION = 'config.wsgi.application' DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.postgresql' , 'NAME' : env( 'DB_NAME' ), 'USER' : env( 'DB_USER' ), 'PASSWORD' : env( 'DB_PASSWORD' ), 'HOST' : env( 'DB_HOST' ), 'PORT' : env( 'DB_PORT' , default= '5432' ), } } # config/settings/development.py from .base import * DEBUG = True ALLOWED_HOSTS = [ 'localhost' , '127.0.0.1' ] DATABASES[ 'default' ][ 'NAME' ] = 'myproject_dev' INSTALLED_APPS += [ 'debug_toolbar' ] MIDDLEWARE += [ 'debug_toolbar.middleware.DebugToolbarMiddleware' ] EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # config/settings/production.py from .base import * DEBUG = False ALLOWED_HOSTS = env. list ( 'ALLOWED_HOSTS' ) SECURE_SSL_REDIRECT = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True SECURE_HSTS_SECONDS = 31536000 SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_PRELOAD = True # Logging LOGGING = { 'version' : 1 , 'disable_existing_loggers' : False , 'handlers' : { 'file' : { 'level' : 'WARNING' , 'class' : 'logging.FileHandler' , 'filename' : '/var/log/django/django.log' , }, }, 'loggers' : { 'django' : { 'handlers' : [ 'file' ], 'level' : 'WARNING' , 'propagate' : True , }, }, } Model Design Patterns Model Best Practices from django.db import models from django.contrib.auth.models import AbstractUser from django.core.validators import MinValueValidator, MaxValueValidator class User ( AbstractUser ): """Custom user model extending AbstractUser.""" email = models.EmailField(unique= True ) phone = models.CharField(max_length= 20 , blank= True ) birth_date = models.DateField(null= True , blank= True ) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [ 'username' ] class Meta : db_table = 'users' verbose_name = 'user' verbose_name_plural = 'users' ordering = [ '-date_joined' ] def __str__ ( self ): return self .email def get_full_name ( self ): return f" {self.first_name} {self.last_name} " .strip() class Product (models.Model): """Product model with proper field configuration.""" name = models.CharField(max_length= 200 ) slug = models.SlugField(unique= True , max_length= 250 ) description = models.TextField(blank= True ) price = models.DecimalField( max_digits= 10 , decimal_places= 2 , validators=[MinValueValidator( 0 )] ) stock = models.PositiveIntegerField(default= 0 ) is_active = models.BooleanField(default= True ) category = models.ForeignKey( 'Category' , on_delete=models.CASCADE, related_name= 'products' ) tags = models.ManyToManyField( 'Tag' , blank= True , related_name= 'products' ) created_at = models.DateTimeField(auto_now_add= True ) updated_at = models.DateTimeField(auto_now= True ) class Meta : db_table = 'products' ordering = [ '-created_at' ] indexes = [ models.Index(fields=[ 'slug' ]), models.Index(fields=[ '-created_at' ]), models.Index(fields=[ 'category' , 'is_active' ]), ] constraints = [ models.CheckConstraint( check=models.Q(price__gte= 0 ), name= 'price_non_negative' ) ] def __str__ ( self ): return self .name def save ( self, *args, **kwargs ): if not self .slug: self .slug = slugify( self .name) super ().save(*args, **kwargs) QuerySet Best Practices from django.db import models class ProductQuerySet (models.QuerySet): """Custom QuerySet for Product model.""" def active ( self ): """Return only active products.""" return self . filter (is_active= True ) def with_category ( self ): """Select related category to avoid N+1 queries.""" return self .select_related( 'category' ) def with_tags ( self ): """Prefetch tags for many-to-many relationship.""" return self .prefetch_related( 'tags' ) def in_stock ( self ): """Return products with stock > 0.""" return self . filter (stock__gt= 0 ) def search ( self, query ): """Search products by name or description.""" return self . filter ( models.Q(name__icontains=query) | models.Q(description__icontains=query) ) class Product (models.Model): # ... fields ... objects = ProductQuerySet.as_manager() # Use custom QuerySet # Usage Product.objects.active().with_category().in_stock() Manager Methods class ProductManager (models.Manager): """Custom manager for complex queries.""" def get_or_none ( self, **kwargs ): """Return object or None instead of DoesNotExist.""" try : return self .get(**kwargs) except self .model.DoesNotExist: return None def create_with_tags ( self, name, price, tag_names ): """Create product with associated tags.""" product = self .create(name=name, price=price) tags = [Tag.objects.get_or_create(name=name)[ 0 ] for name in tag_names] product.tags. set (tags) return product def bulk_update_stock ( self, product_ids, quantity ): """Bulk update stock for multiple products.""" return self . filter (id__in=product_ids).update(stock=quantity) # In model class Product (models.Model): # ... fields ... custom = ProductManager() Django REST Framework Patterns Serializer Patterns from rest_framework import serializers from django.contrib.auth.password_validation import validate_password from .models import Product, User class ProductSerializer (serializers.ModelSerializer): """Serializer for Product model.""" category_name = serializers.CharField(source= 'category.name' , read_only= True ) average_rating = serializers.FloatField(read_only= True ) discount_price = serializers.SerializerMethodField() class Meta : model = Product fields = [ 'id' , 'name' , 'slug' , 'description' , 'price' , 'discount_price' , 'stock' , 'category_name' , 'average_rating' , 'created_at' ] read_only_fields = [ 'id' , 'slug' , 'created_at' ] def get_discount_price ( self, obj ): """Calculate discount price if applicable.""" if hasattr (obj, 'discount' ) and obj.discount: return obj.price * ( 1 - obj.discount.percent / 100 ) return obj.price def validate_price ( self, value ): """Ensure price is non-negative.""" if value < 0 : raise serializers.ValidationError( "Price cannot be negative." ) return value class ProductCreateSerializer (serializers.ModelSerializer): """Serializer for creating products.""" class Meta : model = Product fields = [ 'name' , 'description' , 'price' , 'stock' , 'category' ] def validate ( self, data ): """Custom validation for multiple fields.""" if data[ 'price' ] > 10000 and data[ 'stock' ] > 100 : raise serializers.ValidationError( "Cannot have high-value products with large stock." ) return data class UserRegistrationSerializer (serializers.ModelSerializer): """Serializer for user registration.""" password = serializers.CharField( write_only= True , required= True , validators=[validate_password], style={ 'input_type' : 'password' } ) password_confirm = serializers.CharField(write_only= True , style={ 'input_type' : 'password' }) class Meta : model = User fields = [ 'email' , 'username' , 'password' , 'password_confirm' ] def validate ( self, data ): """Validate passwords match.""" if data[ 'password' ] != data[ 'password_confirm' ]: raise serializers.ValidationError({ "password_confirm" : "Password fields didn't match." }) return data def create ( self, validated_data ): """Create user with hashed password.""" validated_data.pop( 'password_confirm' ) password = validated_data.pop( 'password' ) user = User.objects.create(**validated_data) user.set_password(password) user.save() return user ViewSet Patterns from rest_framework import viewsets, status, filters from rest_framework.decorators import action from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated, IsAdminUser from django_filters.rest_framework import DjangoFilterBackend from .models import Product from .serializers import ProductSerializer, ProductCreateSerializer from .permissions import IsOwnerOrReadOnly from .filters import ProductFilter from .services import ProductService class ProductViewSet (viewsets.ModelViewSet): """ViewSet for Product model.""" queryset = Product.objects.select_related( 'category' ).prefetch_related( 'tags' ) permission_classes = [IsAuthenticated, IsOwnerOrReadOnly] filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter] filterset_class = ProductFilter search_fields = [ 'name' , 'description' ] ordering_fields = [ 'price' , 'created_at' , 'name' ] ordering = [ '-created_at' ] def get_serializer_class ( self ): """Return appropriate serializer based on action.""" if self .action == 'create' : return ProductCreateSerializer return ProductSerializer def perform_create ( self, serializer ): """Save with user context.""" serializer.save(created_by= self .request.user) @action( detail= False , methods=[ 'get' ] ) def featured ( self, request ): """Return featured products.""" featured = self .queryset. filter (is_featured= True )[: 10 ] serializer = self .get_serializer(featured, many= True ) return Response(serializer.data)
Keywords that activate this skill. Click one to copy it.

This skill does not provide trigger words.

The downloaded .skill package contains the following fields.
Field Description
formatFormat tag (skill/v1)
skill_idUnique skill ID
nameSkill name
versionVersion
descriptionDescription
categoryCategories (array)
trigger_wordsTrigger words
tagsTags
sourceSource
source_urlSource URL (this page)
exported_atExported at (set per download)
system_promptSystem prompt body
model_configModel config: provider / model / temperature / max_tokens / top_p
examplesExamples
install_guideImport guide for Coze / Dify / Claude / custom frameworks
The same skill can be exported in different platform formats.
.skill Standard format with system_prompt and model_config, ready for any agent framework Download
.skillpro Enhanced format with scripts, tools, dependencies and hooks Download
.json Plain JSON export with system_prompt and model parameters only Download
Coze Markdown with frontmatter, for Coze platform import Download
Dify Dify DSL, import directly after creating an app Download

每日精选 Skill 推荐,免费送到你邮箱

输入邮箱,每天接收一个精选 AI Agent 技能推荐。完全免费,持续更新。

提交后我们会发送一封确认邮件,点击邮件里的链接才会开始收信。

完全免费,取消任意时间。我们不会发送垃圾邮件。