Add API routes for all models
This commit is contained in:
parent
0e1c5d10fb
commit
f514af6f5f
|
@ -1,7 +1,27 @@
|
||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from server.api import models
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ['username', 'email', 'groups']
|
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__'
|
||||||
|
|
|
@ -1,9 +1,24 @@
|
||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
from server.api import serializers
|
from server.api import serializers, models
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
queryset = User.objects.all().order_by('-date_joined')
|
queryset = User.objects.all().order_by('-date_joined')
|
||||||
serializer_class = serializers.UserSerializer
|
serializer_class = serializers.UserSerializer
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
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]
|
||||||
|
|
|
@ -6,6 +6,9 @@ from .api import views
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'users', views.UserViewSet)
|
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 = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
path('', include(router.urls)),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user