Commit a5cf0c4c authored by Georgios Xonikis's avatar Georgios Xonikis

Initial Views Completed

parent 8f765b90
from django.db import models
# Create your models here.
# Create your models here
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(
......
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'author_id', 'title', 'body', 'created_at']
# read only = cannot be modified:
# read_only_fields = ['id', 'first_name']
# extra_kwargs = {
# 'password': {
# # write only (cant be read afterwards)
# 'write_only': True
# }
# }
from django.contrib import admin
from django.urls import path
from blog.views import view_post_details
from blog.views import (BlogPostsAPIView,
BlogCreatePostAPIView,
UserListPostsAPIView,
BlogPostRetrieveAPIView,
BlogPostDeleteAPIView,
BlogPostUpdateAPIView)
urlpatterns = [
path('', view_post_details), # name='get-all-posts'
path('', BlogPostsAPIView.as_view()),
path('new-post/', BlogCreatePostAPIView.as_view()),
path('posts-by-user-id/<author_id>/', UserListPostsAPIView.as_view()),
path('post-by-post-id/<id>/', BlogPostRetrieveAPIView.as_view()),
path('delete-post-by-post-id/<id>/', BlogPostDeleteAPIView.as_view()),
path('update-post-by-post-id/<id>/', BlogPostUpdateAPIView.as_view()),
#path('<int:post_id>/', GetPostView.as_view(), name='get-post')
]
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views import View
from rest_framework import generics
# import the table from the Model
from .models import Post
# Create your views here.
from .serializers import PostSerializer
'''
Generics Views
'''
class BlogPostsAPIView(generics.ListAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
class BlogCreatePostAPIView(generics.CreateAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
class UserListPostsAPIView(generics.ListAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
def get_queryset(self, *args, **kwargs):
kwargs = self.kwargs
kw_id = kwargs.get('author_id')
return self.queryset.filter(author_id=kw_id)
class BlogPostRetrieveAPIView(generics.RetrieveAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
lookup_field = 'id'
class BlogPostUpdateAPIView(generics.UpdateAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
lookup_field = 'id'
class BlogPostDeleteAPIView(generics.DestroyAPIView):
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
lookup_field = 'id'
'''
Without Serializer
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views import View
def view_post_details(response):
# Store the field in a variable post from a Django query
# type(post) = <class 'django.db.models.query.QuerySet'>
post = Post.objects.values('author', 'title', 'body', 'created_at').all()
# to view all fields
......@@ -20,7 +88,25 @@ def view_post_details(response):
post = list(post)
return JsonResponse(post, safe=False)
'''
'''
API View
from rest_framework.views import APIView
from rest_framework.response import Response
class PostListSearchAPIView(APIView):
permission_classes = []
authentication_classes = []
@staticmethod
def get(request, format = None):
qs = Post.objects.all()
# type(qs) --> <class 'django.db.models.query.QuerySet'>
serializer = PostSerializer(qs, many=True)
# type(serializer) --> <class 'rest_framework.serializers.ListSerializer'>
return Response(serializer.data)
'''
\ No newline at end of file
......@@ -132,8 +132,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'CET'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'UTC'
USE_I18N = True
......@@ -148,10 +147,10 @@ USE_TZ = True
STATIC_URL = '/static/'
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
# REST_FRAMEWORK = {
# # Use Django's standard `django.contrib.auth` permissions,
# # or allow read-only access for unauthenticated users.
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
# ]
# }
......@@ -221,7 +221,7 @@ INSTALLED_APPS = (
)
3.If you're intending to use the browsable API you'll probably also want to add REST framework's login
3. If you're intending to use the browsable API you'll probably also want to add REST framework's login
and logout views. Add the following to your root urls.py file -->
urlpatterns = [
...
......@@ -229,7 +229,7 @@ urlpatterns = [
]
4.Any global settings for a REST framework API are kept in a single
4. Any global settings for a REST framework API are kept in a single
configuration dictionary named REST_FRAMEWORK.
Add the following to the settings.py module -->
REST_FRAMEWORK = {
......@@ -241,5 +241,35 @@ REST_FRAMEWORK = {
}
5. HOW TO CHANGE THE TIMEZONE????????
5. Create a serializers.py in <app name> directory -->
blog/serializers.py -->
from rest_framework import serializers
from .models import <Model class>
class <model class>Serializer(serializers.ModelSerializer):
class Meta:
model = <model class>
fields = ['author', 'title', 'body', 'created_at']
6. Create the Api View in <app name>/views.py
7. Map the Api View in urls.py <app name>/urls.py
8. Check that the app's urls are mapped in projects urls --> app/project/urls.py
############ CREATE SERIALIZER MODULE
############ CREATE GENERICS VIEWS
############ EXTEND USERS TABLE
HOW TO CHANGE THE TIMEZONE ?? --> use local time from python library
WHAT IS META IN MODEL CLASS ??
WHAT IS from django.contrib.auth import get_user_model ??
WHAT IS THE DIFFERENCE BETWEEN VIEW AND GENERIC VIEW?? --> There are different classes, Generic has more packages
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment