This project provides a simple REST API for creating and managing surveys using Django and Django REST Framework (DRF). The core component in this example is the SurveyCreateViewSet, which allows authenticated users to perform CRUD operations on surveys.
your_project/ βββ surveys/ β βββ models.py β βββ serializers.py β βββ views.py β βββ urls.py β βββ ... βββ your_project/ β βββ settings.py βββ manage.py
yaml Copy Edit
- Full CRUD support for surveys
- Authentication required (using DRF's token/session authentication)
- Automatically records the user who created each survey
- Modular and scalable architecture
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .models import Survey
from .serializers import SurveyCreateSerializer
class SurveyCreateViewSet(viewsets.ModelViewSet):
queryset = Survey.objects.all()
serializer_class = SurveyCreateSerializer
permission_classes = [IsAuthenticated]
def perform_create(self, serializer):
serializer.save(created_by=self.request.user)
What it does:
Component Description
ModelViewSet Provides full CRUD operations
queryset Fetches all Survey records
serializer_class Uses SurveyCreateSerializer to validate and serialize data
permission_classes Restricts access to authenticated users only
perform_create Automatically sets created_by to the current user during survey creation
π§ Model: Survey
Example model used by this viewset:
python
Copy
Edit
from django.db import models
from django.contrib.auth.models import User
class Survey(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
π§ͺ Serializer: SurveyCreateSerializer
Example serializer:
python
Copy
Edit
from rest_framework import serializers
from .models import Survey
class SurveyCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Survey
fields = ['id', 'title', 'description', 'created_by', 'created_at']
read_only_fields = ['created_by', 'created_at']