add blog app, config models and views
This commit is contained in:
parent
8540555f6b
commit
0d88ce9d35
5
api/api/blogs/const.py
Normal file
5
api/api/blogs/const.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
# STATUS = (
|
||||||
|
# (0, "Draft"),
|
||||||
|
# (1, "Published")
|
||||||
|
# )
|
@ -1,4 +1,18 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
# move to `const.py`
|
||||||
|
|
||||||
|
STATUS = (
|
||||||
|
(0,"Draft"),
|
||||||
|
(1,"Publish")
|
||||||
|
)
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
class Post(models.Model):
|
||||||
|
title = models.CharField(max_length=200, unique=True)
|
||||||
|
author = models.ForeignKey('user.auth',on_delete=models.CASCADE)
|
||||||
|
updated_on =models.DateTimeField(auto_now= True)
|
||||||
|
created_on = models.DateTimeField(auto_now_add=True)
|
||||||
|
content = models.TextField()
|
||||||
|
status = models.IntegerChoices()
|
6
api/api/blogs/permissions.py
Normal file
6
api/api/blogs/permissions.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from rest_framework import permissions
|
||||||
|
|
||||||
|
class IsOwnerOrReadOnly(permissions.BasePermission):
|
||||||
|
"""
|
||||||
|
Custom permission to only allow owners/authors of the object to edit it.
|
||||||
|
"""
|
10
api/api/blogs/serializers.py
Normal file
10
api/api/blogs/serializers.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
|
from .models import *
|
||||||
|
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
class PostSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Post
|
||||||
|
# better to explicitly mention the fields
|
||||||
|
fields = '__all__'
|
@ -1,7 +1,19 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
from rest_framework import permissions
|
||||||
|
from rest_framework import viewsets
|
||||||
|
|
||||||
|
from .models import *
|
||||||
|
from .serializers import *
|
||||||
|
|
||||||
|
|
||||||
|
# Create your views here.
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
return HttpResponse("You're at the Blogs index")
|
return HttpResponse("You're at the Blogs index")
|
||||||
|
|
||||||
# Create your views here.
|
class PostViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = Post.objects.all()
|
||||||
|
serializer_class = PostSerializer
|
||||||
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||||||
|
Loading…
Reference in New Issue
Block a user