Add API routes for all models

This commit is contained in:
Tanner Collin 2021-04-15 00:02:10 +00:00
parent 0e1c5d10fb
commit f514af6f5f
3 changed files with 39 additions and 1 deletions

View File

@ -1,7 +1,27 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from server.api import models
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'email', 'groups']
class TransactionSerializer(serializers.ModelSerializer):
class Meta:
model = models.Transaction
fields = '__all__'
class StackSerializer(serializers.ModelSerializer):
transactions = TransactionSerializer(many=True, read_only=True)
class Meta:
model = models.Stack
fields = '__all__'
class AccountSerializer(serializers.ModelSerializer):
stacks = StackSerializer(many=True, read_only=True)
class Meta:
model = models.Account
fields = '__all__'

View File

@ -1,9 +1,24 @@
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from server.api import serializers
from server.api import serializers, models
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined')
serializer_class = serializers.UserSerializer
permission_classes = [permissions.IsAuthenticated]
class AccountViewSet(viewsets.ModelViewSet):
queryset = models.Account.objects.all()
serializer_class = serializers.AccountSerializer
permission_classes = [permissions.IsAuthenticated]
class StackViewSet(viewsets.ModelViewSet):
queryset = models.Stack.objects.all()
serializer_class = serializers.StackSerializer
permission_classes = [permissions.IsAuthenticated]
class TransactionViewSet(viewsets.ModelViewSet):
queryset = models.Transaction.objects.all()
serializer_class = serializers.TransactionSerializer
permission_classes = [permissions.IsAuthenticated]

View File

@ -6,6 +6,9 @@ from .api import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'accounts', views.AccountViewSet)
router.register(r'stacks', views.StackViewSet)
router.register(r'transactions', views.TransactionViewSet)
urlpatterns = [
path('', include(router.urls)),