All skills
jeffallan avatar

/django-expert

@2bb8261
by jeffallanjeffallan/claude-skills12k stars
1,124

Use when building Django web applications or REST APIs with Django REST Framework. Invoke when working with settings.py, models.py, manage.py, or any Django project file. Creates Django models with proper indexes, optimizes ORM queries using select_related/prefetch_related, builds DRF serializers and viewsets, and configures JWT authentication. Trigger terms: Django, DRF, Django REST Framework, Django ORM, Django model, serializer, viewset, Python web.

Use this Skill: https://skilld.dev/gh/jeffallan/claude-skills/django-expert

This session only. Nothing lands on disk.

referencestesting-django.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Testing Django

APITestCase

from rest_framework.test import APITestCase
from rest_framework import status
from django.urls import reverse

class ProductAPITest(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            email='test@example.com',
            username='testuser',
            password='testpass123'
        )
        self.category = Category.objects.create(name='Tech', slug='tech')
        self.product = Product.objects.create(
            name='Laptop',
            slug='laptop',
            price=999.99,
            stock=10,
            category=self.category,
            created_by=self.user
        )

    def test_list_products(self):
        url = reverse('product-list')
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 1)

    def test_create_product_authenticated(self):
        self.client.force_authenticate(user=self.user)
        url = reverse('product-list')
        data = {
            'name': 'Phone',
            'price': 499.99,
            'stock': 5,
            'category_id': self.category.id
        }

        response = self.client.post(url, data)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Product.objects.count(), 2)

    def test_create_product_unauthenticated(self):
        url = reverse('product-list')
        response = self.client.post(url, {'name': 'Test'})

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

Model Tests

from django.test import TestCase
from django.core.exceptions import ValidationError

class ProductModelTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            email='test@example.com',
            username='test',
            password='pass'
        )
        self.category = Category.objects.create(name='Tech', slug='tech')

    def test_product_creation(self):
        product = Product.objects.create(
            name='Test Product',
            slug='test-product',
            price=100,
            category=self.category,
            created_by=self.user
        )

        self.assertEqual(str(product), 'Test Product')
        self.assertEqual(product.stock, 0)  # Default

    def test_product_slug_unique(self):
        Product.objects.create(
            name='First', slug='test', price=10,
            category=self.category, created_by=self.user
        )

        with self.assertRaises(Exception):
            Product.objects.create(
                name='Second', slug='test', price=20,
                category=self.category, created_by=self.user
            )

Fixtures

# fixtures/products.json
[
  {
    "model": "products.category",
    "pk": 1,
    "fields": {"name": "Electronics", "slug": "electronics"}
  },
  {
    "model": "products.product",
    "pk": 1,
    "fields": {
      "name": "Laptop",
      "slug": "laptop",
      "price": "999.99",
      "category": 1
    }
  }
]

# In test
class ProductTest(TestCase):
    fixtures = ['products.json']

    def test_with_fixture(self):
        product = Product.objects.get(slug='laptop')
        self.assertEqual(product.name, 'Laptop')

Factory Boy

import factory
from factory.django import DjangoModelFactory

class UserFactory(DjangoModelFactory):
    class Meta:
        model = User

    email = factory.Sequence(lambda n: f'user{n}@example.com')
    username = factory.Sequence(lambda n: f'user{n}')
    password = factory.PostGenerationMethodCall('set_password', 'testpass')

class ProductFactory(DjangoModelFactory):
    class Meta:
        model = Product

    name = factory.Faker('word')
    slug = factory.LazyAttribute(lambda o: slugify(o.name))
    price = factory.Faker('pydecimal', left_digits=3, right_digits=2, positive=True)
    created_by = factory.SubFactory(UserFactory)

# Usage
class ProductTest(TestCase):
    def test_with_factory(self):
        product = ProductFactory(price=100)
        self.assertEqual(product.price, 100)

Testing JWT

class JWTAuthTest(APITestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            email='test@example.com',
            username='test',
            password='testpass123'
        )

    def test_obtain_token(self):
        response = self.client.post('/api/token/', {
            'email': 'test@example.com',
            'password': 'testpass123'
        })

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('access', response.data)
        self.assertIn('refresh', response.data)

    def test_access_protected_endpoint(self):
        response = self.client.post('/api/token/', {
            'email': 'test@example.com',
            'password': 'testpass123'
        })
        token = response.data['access']

        self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}')
        response = self.client.get('/api/protected/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

Quick Reference

Method Purpose
force_authenticate() Skip auth
credentials() Set headers
reverse() URL by name
fixtures Load test data
Assertion Check
assertEqual() Exact match
assertContains() Response contains
assertRaises() Exception raised

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk CRITICAL
  • Gen Agent Trust Hub16d

    A comprehensive Django and Django REST Framework development assistant. The skill promotes security best practices such as avoiding raw SQL, using environment variables for secrets, and implementing proper permissions. It has a low risk of indirect prompt injection because it is designed to analyze and process user-provided code files.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    6 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 2bb8261. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 2 months ago
Other metadata
metadata
{
  "author": "https://github.com/Jeffallan",
  "version": "1.1.0",
  "domain": "backend",
  "triggers": "Django, DRF, Django REST Framework, Django ORM, Django model, serializer, viewset, Python web",
  "role": "specialist",
  "scope": "implementation",
  "output-format": "code",
  "related-skills": "fullstack-guardian, fastapi-expert, test-master, django-storages-s3"
}

README badge

README badge for jeffallan/claude-skills/django-expert

Builds Django REST Framework APIs and models with proper indexing, query optimization via select_related/prefetch_related, DRF serializers with validation, viewsets with permissions, and JWT authentication. Targets Django 5.0 projects and provides workflows for schema design, ORM optimization, and APITestCase testing.

Generated from the current SKILL.md.

Does this skill work with Django 4.x or only Django 5.0?
The skill is built around Django 5.0 and its async views. It may work with Django 4.x but is optimized for Django 5.0 features.
Does this cover Django REST Framework setup and serializers?
Yes. The skill includes DRF serializer design with validation, viewset implementation, and API endpoint configuration with proper permissions.
Does this skill handle authentication?
Yes. It covers JWT authentication via SimpleJWT, session authentication, and permission classes like IsAuthenticatedOrReadOnly.
Will this optimize my existing Django queries?
Yes. The skill emphasizes select_related and prefetch_related to prevent N+1 queries, and recommends database indexes on frequently queried fields.
Does this include testing guidance?
Yes. The skill covers APITestCase, fixtures, and factories for testing Django models and API endpoints, with a reference guide for pytest-django.

Generated from the current SKILL.md. These answers refresh after source changes.