Apakah cuma Instagram ? kalian bisa cek link dibawah:
https://www.quora.com/What-are-some-well-known-sites-running-on-Django
Untuk story-nya tentang django Instagram
http://instagram-engineering.tumblr.com/post/13649370142/what-powers-instagram-hundreds-of-instances
Oke, langsung aja..
Pada kali ini saya akan membahas membuat aplikasi rest service menggunakan module django-rest-framework. Pertama - tama harus mempunya basic pengetahuan tentang struktur project di django dan melakukan konfigurasi django admin site tutorial kalian bisa baca tutorial-django-administration-site.
Install
pip install djangorestframework
Lanjut ke intinya dan pastikan kalian sudah berhasil konfigurasi django admin site. Lalu buat new file /projectname/appname/serializer.py
serializer.py
from django.contrib.auth.models import User, Group from rest_framework import serializers class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ('url', 'username', 'email', 'groups') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = ('url', 'name')
Buka file /projectname/appname/view.py dan tambahkan kode dibawah:
from django.contrib.auth.models import User, Group from rest_framework import viewsets from donate.serializers import UserSerializer, GroupSerializer class UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer class GroupViewSet(viewsets.ModelViewSet): """ API endpoint that allows groups to be viewed or edited. """ queryset = Group.objects.all() serializer_class = GroupSerializer
Register ke /projectname/url.py
from django.conf.urls import url, include from rest_framework import routers from projectname.appname import views router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'groups', views.GroupViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
Langkah terakhir register ke setting.py
INSTALLED_APPS = ( ... 'rest_framework', ) REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',), 'PAGE_SIZE': 10 }
Selesai!. Run server
python manage.py runserverBuka aplikasi dan login menggunakan user admin site:
List API |
Testing Api |
List User halaman admin |
- Sudah ada success story-nya
- Rapid Development
- Banyak community-nya (https://www.djangopackages.com/)
- No python no Django :D
Sekian blog dari saya! Semoga bermanfaat
Sumber:
http://www.django-rest-framework.org/
No comments:
Post a Comment