From bb307d34d25ae17891ef2fd7a7055afa25c0613c Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Thu, 7 Oct 2021 08:59:57 +1300 Subject: [PATCH] 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 --- backend/.gitignore | 3 ++- backend/requirements.txt | 1 + backend/right_tree/api/__init__.py | 0 backend/right_tree/api/admin.py | 3 +++ backend/right_tree/api/apps.py | 5 +++++ .../right_tree/api/migrations/0001_initial.py | 21 +++++++++++++++++++ backend/right_tree/api/migrations/__init__.py | 0 backend/right_tree/api/models.py | 4 ++++ backend/right_tree/api/serializers.py | 9 ++++++++ backend/right_tree/api/views.py | 10 +++++++++ backend/right_tree/settings.py | 4 ++++ backend/right_tree/urls.py | 10 ++++++++- 12 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 backend/right_tree/api/__init__.py create mode 100644 backend/right_tree/api/admin.py create mode 100644 backend/right_tree/api/apps.py create mode 100644 backend/right_tree/api/migrations/0001_initial.py create mode 100644 backend/right_tree/api/migrations/__init__.py create mode 100644 backend/right_tree/api/models.py create mode 100644 backend/right_tree/api/serializers.py create mode 100644 backend/right_tree/api/views.py diff --git a/backend/.gitignore b/backend/.gitignore index c5cdd76..e30397b 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,2 +1,3 @@ *.pyc -*.sqlite3 \ No newline at end of file +*.sqlite3 +__pycache__ \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index b955bfb..bfcbcd9 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,2 +1,3 @@ Django==3.2.8 psycopg2-binary>=2.8 +djangorestframework==3.12.4 diff --git a/backend/right_tree/api/__init__.py b/backend/right_tree/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/right_tree/api/admin.py b/backend/right_tree/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/backend/right_tree/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/right_tree/api/apps.py b/backend/right_tree/api/apps.py new file mode 100644 index 0000000..e86d3a3 --- /dev/null +++ b/backend/right_tree/api/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + name = 'right_tree.api' diff --git a/backend/right_tree/api/migrations/0001_initial.py b/backend/right_tree/api/migrations/0001_initial.py new file mode 100644 index 0000000..14aa118 --- /dev/null +++ b/backend/right_tree/api/migrations/0001_initial.py @@ -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()), + ], + ), + ] diff --git a/backend/right_tree/api/migrations/__init__.py b/backend/right_tree/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/right_tree/api/models.py b/backend/right_tree/api/models.py new file mode 100644 index 0000000..f1ade3a --- /dev/null +++ b/backend/right_tree/api/models.py @@ -0,0 +1,4 @@ +from django.db import models + +class Plant(models.Model): + name = models.TextField() diff --git a/backend/right_tree/api/serializers.py b/backend/right_tree/api/serializers.py new file mode 100644 index 0000000..15a6837 --- /dev/null +++ b/backend/right_tree/api/serializers.py @@ -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'] diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py new file mode 100644 index 0000000..6e96597 --- /dev/null +++ b/backend/right_tree/api/views.py @@ -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 diff --git a/backend/right_tree/settings.py b/backend/right_tree/settings.py index f41437d..5ad4c1f 100644 --- a/backend/right_tree/settings.py +++ b/backend/right_tree/settings.py @@ -37,6 +37,10 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + 'rest_framework', + + 'right_tree.api', ] MIDDLEWARE = [ diff --git a/backend/right_tree/urls.py b/backend/right_tree/urls.py index 4602d73..2b30f2d 100644 --- a/backend/right_tree/urls.py +++ b/backend/right_tree/urls.py @@ -14,8 +14,16 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ 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 = [ path('admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]