Django RESTful Web Services

Book description

Design, build and test RESTful web services with the Django framework and Python

About This Book

  • Create efficient real-world RESTful web services with the latest Django framework
  • Authenticate, secure, and integrate third-party packages efficiently in your Web Services
  • Leverage the power of Python for faster Web Service development

Who This Book Is For

This book is for Python developers who want to create RESTful web services with Django; you need to have a basic working knowledge of Django but no previous experience with RESTful web services is required.

What You Will Learn

  • The best way to build a RESTful Web Service or API with Django and the Django REST Framework
  • Develop complex RESTful APIs from scratch with Django and the Django REST Framework
  • Work with either SQL or NoSQL data sources
  • Design RESTful Web Services based on application requirements
  • Use third-party packages and extensions to perform common tasks
  • Create automated tests for RESTful web services
  • Debug, test, and profile RESTful web services with Django and the Django REST Framework

In Detail

Django is a Python web framework that makes the web development process very easy. It reduces the amount of trivial code, which simplifies the creation of web applications and results in faster development. It is very powerful and a great choice for creating RESTful web services.

If you are a Python developer and want to efficiently create RESTful web services with Django for your apps, then this is the right book for you.

The book starts off by showing you how to install and configure the environment, required software, and tools to create RESTful web services with Django and the Django REST framework. We then move on to working with advanced serialization and migrations to interact with SQLite and non-SQL data sources. We will use the features included in the Django REST framework to improve our simple web service.

Further, we will create API views to process diverse HTTP requests on objects, go through relationships and hyperlinked API management, and then discover the necessary steps to include security and permissions related to data models and APIs. We will also apply throttling rules and run tests to check that versioning works as expected. Next we will run automated tests to improve code coverage.

By the end of the book, you will be able to build RESTful web services with Django.

Style and approach

The book takes a straightforward approach, giving you the techniques and best use cases to build great web services with Django and Python

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  2. Installing the Required Software and Tools
    1. Creating a virtual environment with Python 3.x and PEP 405
      1. Understanding the directory structure for a virtual environment
      2. Activating the virtual environment
      3. Deactivating the virtual environment
    2. Installing Django and Django REST frameworks in an isolated environment
    3. Creating an app with Django
      1. Understanding Django folders, files, and configurations
    4. Installing tools
      1. Installing Curl
      2. Installing HTTPie
      3. Installing the Postman REST client
      4. Installing Stoplight
      5. Installing iCurlHTTP
    5. Test your knowledge
    6. Summary
  3. Working with Models, Migrations, Serialization, and Deserialization
    1. Defining the requirements for our first RESTful Web Service
    2. Creating our first model
    3. Running our initial migration
      1. Understanding migrations
    4. Analyzing the database
      1. Understanding the table generated by Django
    5. Controlling, serialization, and deserialization
    6. Working with the Django shell and diving deeply into serialization and deserialization
    7. Test your knowledge
    8. Summary
  4. Creating API Views
    1. Creating Django views combined with serializer classes
    2. Understanding CRUD operations with Django views and the request methods
    3. Routing URLs to Django views and functions
    4. Launching Django's development server
      1. Making HTTP GET requests that target a collection of instances
      2. Making HTTP GET requests that target a single instance
      3. Making HTTP POST requests
      4. Making HTTP PUT requests
      5. Making HTTP DELETE requests
      6. Making HTTP GET requests with Postman
    5. Making HTTP POST requests with Postman
    6. Test your knowledge
    7. Summary
  5. Using Generalized Behavior from the APIView Class
    1. Taking advantage of model serializers
    2. Understanding accepted and returned content types
    3. Making unsupported HTTP OPTIONS requests with command-line tools
    4. Understanding decorators that work as wrappers
    5. Using decorators to enable different parsers and renderers
    6. Taking advantage of content negotiation classes
    7. Making supported HTTP OPTIONS requests with command-line tools
    8. Working with different content types
    9. Sending HTTP requests with unsupported HTTP verbs
    10. Test your knowledge
    11. Summary
  6. Understanding and Customizing the Browsable API Feature
    1. Understanding the possibility of rendering text/HTML content
    2. Using a web browser to work with our web service
    3. Making HTTP GET requests with the browsable API
    4. Making HTTP POST requests with the browsable API
    5. Making HTTP PUT requests with the browsable API
    6. Making HTTP OPTIONS requests with the browsable API
    7. Making HTTP DELETE requests with the browsable API
    8. Test your knowledge
    9. Summary
  7. Working with Advanced Relationships and Serialization
    1. Defining the requirements for a complex RESTful Web Service
    2. Creating a new app with Django
    3. Configuring a new web service
    4. Defining many-to-one relationships with models.ForeignKey
    5. Installing PostgreSQL
    6. Running migrations that generate relationships
    7. Analyzing the database
    8. Configuring serialization and deserialization with relationships
    9. Defining hyperlinks with serializers.HyperlinkedModelSerializer
    10. Working with class-based views
    11. Taking advantage of generic classes and viewsets
    12. Generalizing and mixing behavior
    13. Working with routing and endpoints
    14. Making requests that interact with resources that have relationships
    15. Test your knowledge
    16. Summary
  8. Using Constraints, Filtering, Searching, Ordering, and Pagination
    1. Browsing the API with resources and relationships
    2. Defining unique constraints
    3. Working with unique constraints
    4. Understanding pagination
    5. Configuring pagination classes
    6. Making requests that paginate results
    7. Working with customized pagination classes
    8. Making requests that use customized paginated results
    9. Configuring filter backend classes
    10. Adding filtering, searching, and ordering
    11. Working with different types of Django filters
    12. Making requests that filter results
    13. Composing requests that filter and order results
    14. Making requests that perform starts with searches
    15. Using the browsable API to test pagination, filtering, searching, and ordering
    16. Test your knowledge
    17. Summary
  9. Securing the API with Authentication and Permissions
    1. Understanding authentication and permissions in Django, the Django REST framework, and RESTful Web Services
    2. Learning about the authentication classes
    3. Including security and permissions-related data to models
    4. Working with object-level permissions via customized permission classes
    5. Saving information about users that make requests
    6. Setting permission policies
    7. Creating the superuser for Django
    8. Creating a user for Django
    9. Making authenticated requests
    10. Making authenticated HTTP PATCH requests with Postman
    11. Browsing the secured API with the required authentication
    12. Working with token-based authentication
    13. Generating and using tokens
    14. Test your knowledge
    15. Summary
  10. Applying Throttling Rules and Versioning Management
    1. Understanding the importance of throttling rules
    2. Learning the purpose of the different throttling classes in the Django REST framework
    3. Configuring throttling policies in the Django REST framework
    4. Running tests to check that throttling policies work as expected
    5. Understanding versioning classes
    6. Configuring a versioning scheme
    7. Running tests to check that versioning works as expected
    8. Test your knowledge
    9. Summary
  11. Automating Tests
    1. Getting ready for unit testing with pytest
    2. Writing unit tests for a RESTful Web Service
    3. Discovering and running unit tests with pytest
    4. Writing new unit tests to improve the tests' code coverage
    5. Running unit tests again with pytest
    6. Test your knowledge
    7. Summary
  12. Solutions
    1. Chapter 1: Installing the Required Software and Tools
    2. Chapter 2: Working with Models, Migrations, Serialization, and Deserialization
    3. Chapter 3: Creating API Views
    4. Chapter 4: Using Generalized Behavior from the APIView Class
    5. Chapter 5: Understanding and Customizing the Browsable API Feature
    6. Chapter 6: Working with Advanced Relationships and Serialization
    7. Chapter 7: Using Constraints, Filtering, Searching, Ordering, and Pagination
    8. Chapter 8: Securing the API with Authentication and Permissions
    9. Chapter 9: Applying Throttling Rules and Versioning Management
    10. Chapter 10: Automating Tests
  13. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Django RESTful Web Services
  • Author(s): Gaston C. Hillar
  • Release date: January 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788833929