This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS = [ | |
'main', | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
# Create your models here. | |
class Book(models.Model): | |
title = models.CharField(max_length=150) | |
author = models.CharField(max_length=100) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from .models import Book | |
# Register your models here. | |
admin.site.register(Book) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS = [ | |
'main', | |
... | |
'rest_framework' | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from main.models import Book | |
class BookModelSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Book | |
fields = '__all__' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import viewsets | |
from .serializer import BookModelSerializer | |
from main.models import Book | |
class BookViewSet(viewsets.ModelViewSet): | |
queryset = Book.objects.all() | |
serializer_class = BookModelSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Books URL Configuration | |
The `urlpatterns` list routes URLs to views. For more information please see: | |
https://docs.djangoproject.com/en/3.0/topics/http/urls/ | |
Examples: | |
Function views | |
1. Add an import: from my_app import views | |
2. Add a URL to urlpatterns: path('', views.home, name='home') | |
Class-based views | |
1. Add an import: from other_app.views import Home |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import include, path | |
from rest_framework import routers | |
from .views import BookViewSet | |
router = routers.DefaultRouter() | |
router.register(r'books', BookViewSet) | |
urlpatterns = [ | |
path('', include(router.urls)), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def create_book(title, author): | |
headers={ | |
'title':title, | |
'author':author | |
} | |
response = requests.post('http://127.0.0.1:8000/books/', headers) | |
print(f"New book {title} by {author} has been created,\n\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.postgresql_psycopg2', | |
'NAME': 'sample', # DATABASE NAME | |
'USER': 'postgres', # NAME OF USER (DEFAULT IS postgres) | |
'PASSWORD': 'xyz', # whatever password you have kept. | |
'HOST': '127.0.0.1', # HOST in which the psql server is running on | |
'PORT': '5432', # PORT in which the psql server is running on | |
} | |
} |
OlderNewer