Add django rest framework and basic plant model

- Adds django rest framework to requirements
- Adds basic plant model with id and name fields
- Adds CRUD endpoints to interact with plant objects
This commit is contained in:
Dana Lambert 2021-10-07 08:59:57 +13:00
parent aedd224a63
commit bb307d34d2
12 changed files with 68 additions and 2 deletions

1
backend/.gitignore vendored
View file

@ -1,2 +1,3 @@
*.pyc *.pyc
*.sqlite3 *.sqlite3
__pycache__

View file

@ -1,2 +1,3 @@
Django==3.2.8 Django==3.2.8
psycopg2-binary>=2.8 psycopg2-binary>=2.8
djangorestframework==3.12.4

View file

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'right_tree.api'

View file

@ -0,0 +1,21 @@
# Generated by Django 3.2.8 on 2021-10-06 18:32
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Plant',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.TextField()),
],
),
]

View file

@ -0,0 +1,4 @@
from django.db import models
class Plant(models.Model):
name = models.TextField()

View file

@ -0,0 +1,9 @@
from rest_framework import serializers
from right_tree.api.models import Plant
class PlantSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = Plant
fields = ['id', 'name']

View file

@ -0,0 +1,10 @@
from rest_framework import viewsets
from right_tree.api.models import Plant
from right_tree.api.serializers import PlantSerializer
class PlantViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows hours to be grouped or edited.
"""
queryset = Plant.objects.all()
serializer_class = PlantSerializer

View file

@ -37,6 +37,10 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework',
'right_tree.api',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View file

@ -14,8 +14,16 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
from rest_framework import routers
from right_tree.api import views
router = routers.DefaultRouter()
router.register(r'plants', views.PlantViewSet)
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
] ]