Saturday, 15 October 2022

Django rest framework Project tutorials part 3

psc project

psc_project

-pscproject

--settiṇgs.py

--urls.py

-questionapp

--models.py

--urls.py

--serializers.py

--views.py

-userapp

--models.py

--urls.py

--serializers.py

--views.py

-manage.py

  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'userapp',
    'questionapp',
]

  urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('userapp.urls')),
    path('',include('questionapp.urls')),
]

Question app

-models.py

-serilizers.py

-urls.py

-views.py

-models.py

from django.db import models

# Create your models here.
class Question(models.Model):
    question=models.CharField(max_length=255)
    ans1=models.CharField(max_length=255)
    ans2=models.CharField(max_length=255)
    ans3=models.CharField(max_length=255,blank=True,null=True)
    ans4=models.CharField(max_length=255,blank=True,null=True)
    ans5=models.CharField(max_length=255,blank=True,null=True)
    ans6=models.CharField(max_length=255,blank=True,null=True)
    original_ans=models.PositiveIntegerField(default=0)
    grade=models.FloatField
    class Meta:
        db_table='questions'

serilizers.py

from questionapp.models import Question
from rest_framework import serializers

class QuestionSerilizer(serializers.ModelSerializer):

    class Meta:
        model =Question
        fields = '__all__'
class QuestionDetailSerilizer(serializers.ModelSerializer):
   
    class Meta:
        model =Question
        fields = ('id','question')

views.py

from questionapp.models import Question
from rest_framework.response import Response
from rest_framework import viewsets
from questionapp.serilizers import QuestionDetailSerilizer, QuestionSerilizer

class QuestionViewset(viewsets.ModelViewSet):
    queryset =Question.objects.all()
    serializer_class = QuestionSerilizer
   
    def list(self, request, *args, **kwargs):
        queryset=self.queryset
        data= QuestionDetailSerilizer(queryset,many=True).data
        return Response(data)

urls.py

from questionapp.views import QuestionViewset
from rest_framework import routers
router=routers.SimpleRouter()
router.register("question",QuestionViewset)
urlpatterns=router.urls


USERAPP

-models.py

-serilizers.py

-urls.py

-views.py


models.py

from django.db import models
from django.contrib.auth.models import User

class Profile(User):
    qualification= models.CharField(max_length=255,blank= True,null=True)
    location= models.CharField(max_length=255,blank=True,null=True)
    address= models.TextField(blank= True,null=True)
    country= models.CharField(max_length=100,blank=True,null=True)
    district= models.CharField(max_length= 100,blank=True,null=True)
    photo= models.ImageField(blank=True,null=True)
   
    class Meta:
        db_table='profiles'


serilizers.py

from userapp.models import Profile
from rest_framework import serializers

class ProfileSerilizer(serializers.ModelSerializer):
   
    class Meta:
        model =Profile
        fields = '__all__'
class ProfileDetailSerilizer(serializers.ModelSerializer):
   
    class Meta:
        model =Profile
        fields = ('id','username')

views.py

from userapp.models import Profile
from rest_framework.response import Response
from rest_framework import viewsets
from userapp.serializers import ProfileDetailSerilizer, ProfileSerilizer

class ProfileViewset(viewsets.ModelViewSet):
    queryset =Profile.objects.all()
    serializer_class = ProfileSerilizer
   
    def list(self, request, *args, **kwargs):
        queryset=self.queryset
        data= ProfileDetailSerilizer(queryset,many=True).data
        return Response(data)

urls.py

from userapp.views import ProfileViewset

from rest_framework import routers
router=routers.SimpleRouter()
router.register("profile",ProfileViewset)
urlpatterns=router.urls













No comments:

Post a Comment