8. create serializers.py in app ie., quickstart ,update (view.py,url.py,setting.py) and check the rest api after runserver


ok write the following in terminal....



(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py syncdb
/home/erp/tutorial/env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.

You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'erp'): erp
Email address: erp@gmail.com
Password:
Password (again):
Superuser created successfully.
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py runserver


 


o/p








why we are seeing this is becuase we will write serializer code for admin only ie.,

user and group





create in app ie., quickstart with the file name 


serializers.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')




View.py
-----------

from django.shortcuts import render
from django.contrib.auth.models import User,Group
from rest_framework import viewsets
from quickstart.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
      queryset = Group.objects.all()
      serializer_class = GroupSerializer

url.py
=====
from django.conf.urls import patterns, include, url
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups',views.GroupViewSet)


urlpatterns = patterns('',
    # Examples:
     url(r'^', include(router.urls)),
     url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),


)
 
 
 
setting.py
---------
 
 
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'quickstart',
    'rest_framework',
)

REST_FRAMEWORK = {
       'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
       'PAGINATE_BY':10
}
 
 
 
 
 
in terminal
 
====
 
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
June 12, 2015 - 09:53:28
Django version 1.8.2, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.




 




No comments:

Post a Comment