Convert Model object to an API-appropriate format using ModelSerializer class. In api/bonebudget/serializers.py:
from rest_framework import serializers
from .models import Transaction
class TransactionSerializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = [
"id", "user_id", "is_credit", "transaction_type", "amount", "posted_date", "status", "description", "created_at", "updated_at"
]
Views To Do:
Import:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import permissions # authenticated users only
from .models import Transaction
from .serializers import TransactionSerializer
transactions/api/:
Create TransactionView class in api/bonebudget/views.py
class TransactionView(APIView):
permission_classes = [permissions.IsAuthenticated]
def get(self):
pass # add logic here
def post(self):
pass # add logic here
Create an endpoint for the class-based view in api/bonebudget/urls.py:
cc @AshMudra for visibility. Here's what I'm following: https://blog.logrocket.com/django-rest-framework-create-api/, and what I'm going to work on this week.
Setup To Do:
'rest_framework'
toINSTALLED_APPS
pip install django_rest_framework
pip freeze > requirements.txt
# add django_rest_framework to requirements.txtserializers.py
inapi/bonebudget/..
path('api-auth/', include('rest_framework.urls')),
to api/api/urls.pypython manage.py createsuperuser
Endpoint:
bonebudget/transaction/
Endpoint:
bonebudget/transaction/<uid:transaction_id>
Models To Do:
python manage.py makemigrations
&python manage.py migrate
Convert Model object to an API-appropriate format using ModelSerializer class. In api/bonebudget/serializers.py:
Views To Do:
transactions/api/
:api/bonebudget/views.py
Create an endpoint for the class-based view in
api/bonebudget/urls.py
:add a url to
api/bonebudget/urls.py
:python manage.py runserver
Navigate to http://127.0.0.1:8000/transaction/api/ to test.
Try to add a transaction:
transaction/api/:
api/bonebudget/views.py
Add a url to
api/bonebudget/urls.py
Import:
navigate to http://127.0.0.1:8000/transactions/bonebudget// to test using a valid
transaction_id
.