One-to-One, One-to-Many, Many-to-Many0%

One-to-One, One-to-Many, Many-to-Many

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

DRF Relational Fields: Primary Key, Slug, and Hyperlinked

In relational architectures, models reference other models. Serializing these associations in Django REST Framework requires selecting the appropriate relational field style: whether to represent related models as Primary Keys, Human-Readable Slugs, Hyperlinked URLs, or Nested JSON Objects.


1. Relational Field Styles Comparison

Relational FieldOutput FormatBest Use Case
PrimaryKeyRelatedField{"category": 5}Standard CRUD, high write efficiency
SlugRelatedField{"category": "cloud-architecture"}Clean human-readable identifiers
HyperlinkedRelatedField{"category": "https://api.com/v1/categories/5/"}Pure RESTful HATEOAS systems
StringRelatedField{"category": "Cloud Architecture"}Read-only __str__ representations
Nested Serializer{"category": {"id": 5, "name": "Cloud"}}Detailed nested object graphs

2. Implementing Relational Fields in Code

Python
# articles/serializers.py
from rest_framework import serializers
from .models import Article, Category, Tag
 
class ArticleRelationalSerializer(serializers.ModelSerializer):
# 1. PrimaryKeyRelatedField: Client passes ID integer
category_id = serializers.PrimaryKeyRelatedField(
queryset=Category.objects.all(),
source="category",
write_only=True
)
 
# 2. SlugRelatedField: Uses human-readable unique slug
category_slug = serializers.SlugRelatedField(
slug_field="slug",
read_only=True,
source="category"
)
 
# 3. StringRelatedField: Read-only representation using model __str__
author_name = serializers.StringRelatedField(source="author", read_only=True)
 
# 4. ManyToMany field via SlugRelatedField
tags = serializers.SlugRelatedField(
many=True,
slug_field="title",
queryset=Tag.objects.all()
)
 
# 5. HyperlinkedRelatedField: Output full URL to detail view
url = serializers.HyperlinkedIdentityField(
view_name="article-detail",
lookup_field="slug"
)
 
class Meta:
model = Article
fields = [
"url",
"id",
"title",
"slug",
"author_name",
"category_slug",
"category_id",
"tags",
]

Serialized JSON Output:

JSON
{
"url": "https://api.enterprise.com/v1/articles/modern-django/",
"id": 101,
"title": "Modern Django Architecture",
"slug": "modern-django",
"author_name": "Dr. Sumit",
"category_slug": "backend-engineering",
"tags": ["python", "django", "drf"]
}

3. The HyperlinkedModelSerializer

Instead of returning integer primary keys, HyperlinkedModelSerializer automatically uses HyperlinkedRelatedField for all relationships and includes a url field:

Python
class CategoryHyperlinkedSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
fields = ["url", "id", "name", "slug"]
extra_kwargs = {
"url": {"view_name": "category-detail", "lookup_field": "slug"}
}

Notice: When using HyperlinkedModelSerializer, the serializer context must include the request object (which ViewSets do automatically):

Python
serializer = CategoryHyperlinkedSerializer(instance, context={"request": request})

Interactive Quiz

Practice Quiz

Question 1
What is the output of a PrimaryKeyRelatedField?
Question 2
What requirement must a model field satisfy to be used in a SlugRelatedField?
Question 3
Why is context={'request': request} required when initializing a HyperlinkedModelSerializer?
Question 4
What is the behavior of StringRelatedField?
Question 5
When writing to a relationship using SlugRelatedField(slug_field="name", queryset=Tag.objects.all()), what does DRF do?
Next Lesson

Nested Serializers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Pagination TechniquesNested Serializers

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext