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:
parent
aedd224a63
commit
bb307d34d2
12 changed files with 68 additions and 2 deletions
3
backend/.gitignore
vendored
3
backend/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
*.pyc
|
||||
*.sqlite3
|
||||
*.sqlite3
|
||||
__pycache__
|
|
@ -1,2 +1,3 @@
|
|||
Django==3.2.8
|
||||
psycopg2-binary>=2.8
|
||||
djangorestframework==3.12.4
|
||||
|
|
0
backend/right_tree/api/__init__.py
Normal file
0
backend/right_tree/api/__init__.py
Normal file
3
backend/right_tree/api/admin.py
Normal file
3
backend/right_tree/api/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
backend/right_tree/api/apps.py
Normal file
5
backend/right_tree/api/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
name = 'right_tree.api'
|
21
backend/right_tree/api/migrations/0001_initial.py
Normal file
21
backend/right_tree/api/migrations/0001_initial.py
Normal 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()),
|
||||
],
|
||||
),
|
||||
]
|
0
backend/right_tree/api/migrations/__init__.py
Normal file
0
backend/right_tree/api/migrations/__init__.py
Normal file
4
backend/right_tree/api/models.py
Normal file
4
backend/right_tree/api/models.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from django.db import models
|
||||
|
||||
class Plant(models.Model):
|
||||
name = models.TextField()
|
9
backend/right_tree/api/serializers.py
Normal file
9
backend/right_tree/api/serializers.py
Normal 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']
|
10
backend/right_tree/api/views.py
Normal file
10
backend/right_tree/api/views.py
Normal 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
|
|
@ -37,6 +37,10 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
|
||||
'rest_framework',
|
||||
|
||||
'right_tree.api',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
|
@ -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'))
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue