User = models.ForeignKey(User, on_delete = models.CASCADE, blank = True, null = True)Īfter creating the model, migrate it to the database. Updated = models.DateTimeField(auto_now = True, blank = True) Timestamp = models.DateTimeField(auto_now_add = True, auto_now = False, blank = True)Ĭompleted = models.BooleanField(default = False, blank = True) Task = models.CharField(max_length = 180) Let’s start by creating the model for our to-do list: # todo/todo_api/models.pyįrom import User Delete: Delete a to-do with given todo_id Update: Update a to-do with given todo_idĥ. Retrieve: Get a to-do with given todo_idĤ. List All: List all to-dos for requested userģ. We’ll use two endpoints with their respective HTTP methods, as shown in the table below: Endpointġ. To show how to build a RESTful app with Django REST framework, we’ll create an example to-do API. You must organize these methods logically. In a RESTful API, endpoints define the structure and usage with the GET, POST, PUT, and DELETE HTTP methods. RESTful structure: GET, POST, PUT, and DELETE methods We’ll come back to this later: python manage.py createsuperuser Path('api-auth/', include('rest_framework.urls')), Next, add rest_framework and todo to the INSTALLED_APPS inside the todo/todo/settings.py file: # settings.pyĬreate a serializers.py and urls.py file in todo/todo_api and add new files as configured in the directory structure below: ├── todoīe sure to include rest_framework and URLs as shown below in your main urls.py file: # todo/todo/urls.py : Main urls.py Run your initial migrations of the built-in user model: python manage.py migrate Then, cd into the new todo folder and create a new app for your API: django-admin startapp todo_api Navigate to an empty folder in your terminal and install Django and Django REST framework in your project with the commands below: pip install djangoĬreate a Django project called todo with the following command: django-admin startproject todo Keep in mind that you’ll need to reactivate your virtual environment in every new terminal session. You’ll know that it is turned on because the environment’s name will become part of the shell prompt. Run the command python -m venv django_env from inside your projects folder to create the virtual environment. Ideally, you’d want to create a virtual environment to isolate dependencies, however, this is optional. It adopts implementations like class-based views, forms, model validator, QuerySet, and more. Its main benefit is that it makes serialization much easier.ĭjango REST framework is based on Django’s class-based views, so it’s an excellent option if you’re familiar with Django. Why Django REST framework?ĭjango REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. REST, which stands for representational state transfer, can be made up of one or more resources that can be accessed at a given URL and returned in various formats, like JSON, images, HTML, and more. What is a REST API?Ī REST API is a popular way for systems to expose useful functions and data. It reduces the hassle of web development so that you can focus on writing your app instead of reinventing the wheel.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |