Skip to content

Instantly share code, notes, and snippets.

View shaurya-blip's full-sized avatar
✔️
Dead Bot Developer

SblipDev shaurya-blip

✔️
Dead Bot Developer
View GitHub Profile
INSTALLED_APPS = [
'main',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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)
from django.contrib import admin
from .models import Book
# Register your models here.
admin.site.register(Book)
INSTALLED_APPS = [
'main',
...
'rest_framework'
]
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
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)),
from rest_framework import serializers
from main.models import Book
class BookModelSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
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")
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
}
}
# Coefficients
print('Coefficients: \n', regressor.coef_)
# The mean squared error
print("Mean squared error: %.2f" % np.mean((regressor.predict(X_test) - y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regressor.score(X_test, y_test))