Skip to main content

Django

很久之前寫的東西

rest_framework

serializers.py

from django.db.models import fields
from rest_framework import serializers
from .models import ...

class ...Serializer(serializers.ModelSerializer):
class Meta:
model = ...
fields = [
...
]

urls.py

from rest_framework.routers import DefaultRouter
from .views import ...

router = DefaultRouter()
router.register(r'...', ...ViewSet, basename='...')
# Need basename if no define queryset (user may define get_queryset), else ignore

urlpatterns = router.urls

view.py

from django.shortcuts import render

from rest_framework import viewsets

from .models import ...
from .serializers import ...

class ...ViewSet(viewsets.ModelViewSet):
queryset = ... .objects.all()
serializer_class = ...

Change id of get url/<id>

class ...ViewSet(viewsets.ModelViewSet):
queryset = ...
serializer_class = ...
lookup_field = '<field>' # add this