django
Differences
This shows you the differences between two versions of the page.
| django [2026/03/27 00:36] – Create Django snippets page with common patterns roger | django [2026/03/27 00:52] (current) – Add migrations, signals, and DRF sections roger | ||
|---|---|---|---|
| Line 165: | Line 165: | ||
| </ | </ | ||
| + | |||
| + | ===== Migrations ===== | ||
| + | |||
| + | ==== Squash migrations ==== | ||
| + | |||
| + | <code bash> | ||
| + | python manage.py squashmigrations myapp 0001 0015 | ||
| + | </ | ||
| + | |||
| + | ==== Create an empty migration (for data migrations) ==== | ||
| + | |||
| + | <code bash> | ||
| + | python manage.py makemigrations myapp --empty -n my_data_migration | ||
| + | </ | ||
| + | |||
| + | ==== Data migration template ==== | ||
| + | |||
| + | <code python> | ||
| + | from django.db import migrations | ||
| + | |||
| + | |||
| + | def forwards(apps, | ||
| + | MyModel = apps.get_model(" | ||
| + | for obj in MyModel.objects.filter(status=" | ||
| + | obj.status = " | ||
| + | obj.save(update_fields=[" | ||
| + | |||
| + | |||
| + | def backwards(apps, | ||
| + | pass # or reverse the migration | ||
| + | |||
| + | |||
| + | class Migration(migrations.Migration): | ||
| + | dependencies = [ | ||
| + | (" | ||
| + | ] | ||
| + | |||
| + | operations = [ | ||
| + | migrations.RunPython(forwards, | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | ===== Signals ===== | ||
| + | |||
| + | ==== Post-save signal ==== | ||
| + | |||
| + | <code python> | ||
| + | from django.db.models.signals import post_save | ||
| + | from django.dispatch import receiver | ||
| + | |||
| + | @receiver(post_save, | ||
| + | def create_profile(sender, | ||
| + | if created: | ||
| + | Profile.objects.create(user=instance) | ||
| + | </ | ||
| + | |||
| + | ===== Django REST Framework ===== | ||
| + | |||
| + | ==== Serializer with nested write support ==== | ||
| + | |||
| + | <code python> | ||
| + | class AddressSerializer(serializers.ModelSerializer): | ||
| + | class Meta: | ||
| + | model = Address | ||
| + | fields = [" | ||
| + | |||
| + | |||
| + | class UserSerializer(serializers.ModelSerializer): | ||
| + | address = AddressSerializer() | ||
| + | |||
| + | class Meta: | ||
| + | model = User | ||
| + | fields = [" | ||
| + | |||
| + | def create(self, | ||
| + | address_data = validated_data.pop(" | ||
| + | user = User.objects.create(**validated_data) | ||
| + | Address.objects.create(user=user, | ||
| + | return user | ||
| + | </ | ||
| + | |||
| + | ==== Custom permission ==== | ||
| + | |||
| + | <code python> | ||
| + | from rest_framework.permissions import BasePermission | ||
| + | |||
| + | class IsOwner(BasePermission): | ||
| + | def has_object_permission(self, | ||
| + | return obj.owner == request.user | ||
| + | </ | ||
| + | |||
| + | ==== Pagination ==== | ||
| + | |||
| + | <code python> | ||
| + | # settings.py | ||
| + | REST_FRAMEWORK = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | # Or custom pagination | ||
| + | from rest_framework.pagination import CursorPagination | ||
| + | |||
| + | class CreatedAtCursorPagination(CursorPagination): | ||
| + | ordering = " | ||
| + | page_size = 50 | ||
| + | </ | ||
| ===== See Also ===== | ===== See Also ===== | ||
| * [[django: | * [[django: | ||
django.1774571813.txt.gz · Last modified: 2026/03/27 00:36 by roger