I want to acheive: I have 2 Models Tag and Startup.
my models.py file -
from django.db import models
from django_extensions.db.fields import AutoSlugField
from django.db.models import CharField, TextField, DateField, EmailField, ManyToManyField
class Tag(models.Model):
name = CharField(max_length=31, unique=True, default="tag-django")
slug = AutoSlugField(max_length=31, unique=True, populate_from=["name"])
def __str__(self):
return self.name
class Startup(models.Model):
name = CharField(max_length=31, db_index=True)
slug = AutoSlugField(max_length=31, unique=True, populate_from=["name"])
description = TextField()
date_founded = DateField(auto_now_add=True)
contact = EmailField()
tags = ManyToManyField(Tag, related_name="tags")
class Meta:
get_latest_by = ["date_founded"]
def __str__(self):
return self.name
When creating a startup, I have a dropdown list of created Tags which I want to relate to my Startup that I am creating. When I am posting the data -
{
"name": "Startup4",
"description": "TestStartup",
"contact": "startuptest@gmail.com",
"tags": [
{
"url": "http://127.0.0.1:8000/api/v1/tag/first-tag/",
"name": "First Tag",
"slug": "first-tag"
},
{
"url": "http://127.0.0.1:8000/api/v1/tag/second-tag/",
"name": "Second Tag",
"slug": "second-tag"
},
{
"url": "http://127.0.0.1:8000/api/v1/tag/third-tag/",
"name": "Third Tag",
"slug": "third-tag"
}
]
}
A startup is created but the tags field remains empty. No Tags are related.
My serializers.py file -
from rest_framework.serializers import HyperlinkedModelSerializer, PrimaryKeyRelatedField, ModelSerializer
from .models import Startup, Tag
class TagSerializer(HyperlinkedModelSerializer):
class Meta:
model = Tag
fields = "__all__"
extra_kwargs = {
"url": {
"lookup_field": "slug",
"view_name": "tag-api-detail"
}
}
class StartupSerializer(HyperlinkedModelSerializer):
tags = TagSerializer(many=True, read_only=False)
class Meta:
model = Startup
fields = "__all__"
extra_kwargs = {
"url": {
"lookup_field": "slug",
"view_name": "startup-api-detail"
}
}
My viewsets.py file -
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from .serializers import TagSerializer, StartupSerializer
from .models import Tag, Startup
from rest_framework.decorators import action
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_200_OK, HTTP_204_NO_CONTENT
from django.shortcuts import get_object_or_404
class TagViewSet(ModelViewSet):
queryset = Tag.objects.all()
serializer_class = TagSerializer
lookup_field = "slug"
class StartupViewSet(ModelViewSet):
serializer_class = StartupSerializer
queryset = Startup.objects.all()
lookup_field = "slug"
@action(detail=True, methods=["HEAD", "GET", "POST"], url_path="tags")
def tags(self, request, slug=None):
# relate a Posted Tag
startup = self.get_object()
print(startup)
if request.method in ("HEAD", "GET"):
s_tag = TagSerializer(
startup.tags,
many=True,
context={"request": request}
)
return Response(s_tag.data)
tag_slug = request.data.get("slug")
if not tag_slug:
return Response(
"Slug of Tag must be specified",
status=HTTP_400_BAD_REQUEST
)
tag = get_object_or_404(Tag, slug__iexact=tag_slug)
startup.tags.add(tag)
startup.save()
return Response(HTTP_204_NO_CONTENT)
I do not understand where is the problem. I am using @action decorator to relate my POSTed tags.
I have used @action decorator (https://www.django-rest-framework.org/api-guide/viewsets/) to make .../startup/<startup:slug>/tags/
routable so the I have to hit the tags endpoint of the startup with the tag data to relate tags with that startup. In my frontend (Using React) I used 2 post requests to acheive the same. I created the startup hitting the startup endpoint with startup data and then .../startup/<startup:slug>/tags/
endpoint with the tag data.
I realized it later that I have to send the tag data in the following related api endpoint.