ModelViewSet ve Yönlendiriciler
CRUD rotalarını otomatik olarak oluşturun.
ModelViewSet ve Yönlendiriciler, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
The CRUD Repetition Problem
Writing list, create, retrieve, update, and delete by hand means a lot of near-identical code. A ViewSet bundles all of it into one tidy class.
Meet ModelViewSet
The ModelViewSet gives you full CRUD for a model with almost no code. Point it at a queryset and a serializer, and it handles the rest.
from rest_framework import viewsets
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializerTwo Attributes Do the Work
Those two lines carry the whole class. queryset says which rows are in play, and serializer_class says how to turn them into JSON. 📦
The Actions You Get Free
A ModelViewSet provides five actions: list, create, retrieve, update, partial_update, and destroy. You wrote none of them by hand.
Why Routers Exist
A ViewSet groups actions, but URLs still need wiring. A router reads your ViewSet and generates every URL pattern automatically.
Registering with DefaultRouter
Create a DefaultRouter, register your ViewSet under a prefix, and it builds the full URL set for you.
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'books', BookViewSet)Plugging Router URLs In
The router exposes router.urls, a ready list of patterns. Add it to your urlpatterns and the endpoints go live instantly.
urlpatterns = [
path('api/', include(router.urls)),
]The URLs You Just Created
Registering one ViewSet gives you /books/ for the list and create, plus /books/{id}/ for retrieve, update, and delete. 🚀
HTTP Verbs Map to Actions
The router maps verbs to actions: GET lists, POST creates, GET on an id retrieves, PUT or PATCH updates, and DELETE destroys. One ViewSet, six behaviors.
DefaultRouter vs SimpleRouter
DefaultRouter adds a handy API root view that lists your endpoints. SimpleRouter skips it when you want a leaner URL set.
Adding a Custom Action
Need an extra endpoint? The @action decorator adds a custom route to a ViewSet without leaving the class.
from rest_framework.decorators import action
@action(detail=True, methods=['post'])
def favorite(self, request, pk=None):
...Quick Check
Time to lock in how ViewSets and routers fit together.
Recap: CRUD in a Few Lines
You learned that a ModelViewSet plus a router turns two short classes into a complete CRUD API, with clean URLs generated for you. Nicely done! 🎉
Sıkça Sorulan Sorular
“ModelViewSet ve Yönlendiriciler” dersi ücretsiz mi?
Evet — “ModelViewSet ve Yönlendiriciler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.
“ModelViewSet ve Yönlendiriciler” dersinde ne öğreneceğim?
CRUD rotalarını otomatik olarak oluşturun. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Django Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“ModelViewSet ve Yönlendiriciler” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- ModelViewSet ve Yönlendiriciler
- İzinler ve Hız Sınırlama
- Token ve JWT Kimlik Doğrulaması
- Filtreleme, Arama ve Sayfalama