|
| 1 | +# Build an API with Django REST Framework |
| 2 | + |
| 3 | +## Overview |
| 4 | +For this project, we will be creating a functioning REST API. REST APIs can help distribute useful information via GET requests, as well as post and alter databases in a user friendly fashion. |
| 5 | + |
| 6 | +This project will revolve around using Django and Django's REST framework to build an API for the dataset of your choice. Django is a full web framework capable of handling both back and front end portions of a web app; and the Django team has created great resources to make setting up a Django app quick and easy. |
| 7 | + |
| 8 | +While the project is structured around Django, feel free to use flask instead, if you're more comfortable. |
| 9 | + |
| 10 | +## Environment Setup |
| 11 | +To avoid bloating of your primary working environment, we strongly recommend creating a virtual environment. The requirements.txt file includes the required packages, and the included versions have been tested for our needs - use different versions at your own risk. |
| 12 | + |
| 13 | +We also strong recommend using [Atom](https://atom.io/) or [Sublime Text](https://www.sublimetext.com/3) as your text editor. This project has also NOT been tested using Jupyter Notebook, PyCharm, |
| 14 | +Spider, or any other ide/text editor/programming environment. |
| 15 | + |
| 16 | +1. For this challenge you will need Python 3.7, pipenv, and git installed. If you're not familiar with pipenv, it's a packaing tool for Python that effectively replaced the pip+virtualenv+requirements.txt workflow. If you already have pip installed, the easiest way to install pipenv is with `pip install --user pipenv`; however, a better way for Mac/Linux Homebrew users is to instead run `brew install pipenv`. More options can be found [here](https://pipenv-fork.readthedocs.io/en/latest/install.html#installing-pipenv). |
| 17 | + |
| 18 | +2. The project is in the ChiPy project night repo. If you do not have the repository already, run |
| 19 | + |
| 20 | + ``` |
| 21 | + git clone https://github.com/chicagopython/CodingWorkshops.git |
| 22 | + ``` |
| 23 | + |
| 24 | +3. Navigate to the folder for this challenge: |
| 25 | + |
| 26 | + ``` |
| 27 | + cd CodingWorkshops/problems/webdev/django_rest_framework_api |
| 28 | + ``` |
| 29 | + |
| 30 | +4. Run `pipenv install`, which will install all of the libraries we have recommended for this exercise. |
| 31 | +5. After you've installed all of the libraries, run `pipenv shell`, which will turn on a virtual environment running Python 3.7. |
| 32 | +6. To exit the pipenv shell when you are done, simply type `exit`. |
| 33 | + |
| 34 | +## Instructions |
| 35 | + |
| 36 | +### Find a Database |
| 37 | +- Before advancing, find a database that you wish to use for a REST API. It helps if the data is something you are interested in, but don't waste too much time on this part. [Kaggle](https://www.kaggle.com/tags/databases) has a great selection of publicly available databases. If you are looking for something specific, Google has a stellar [database search](https://toolbox.google.com/datasetsearch) feature. |
| 38 | + |
| 39 | +### Create Your First App |
| 40 | + |
| 41 | +- Create a Django app in a local directory of your choosing. Feel free to use the [Django tutorial]((https://docs.djangoproject.com/en/2.2/intro/tutorial01/)) to accomplish this, but please don't call your app the standard Polls App. Create a unique application inside of your Django directory to handle your database and models. Make sure the application is configured in your settings.py file! |
| 42 | + |
| 43 | +### Create a Django Model |
| 44 | +- Create a Django model custom to your database. Feel free to take liberties like creating relational databases for your models. The model field types should match the intended fields of your database. Make sure to migrate your Django model when you are finished! |
| 45 | + |
| 46 | +### Configure the REST framework |
| 47 | +- Make sure you appropriately configure Django REST Framework in your settings.py file. If you forget this step, Django to recognize the add on. |
| 48 | + |
| 49 | +### Serialization |
| 50 | +- Before creating a url or view, serialize your data. This allows Django to render data into a JSON format. Make sure you designate the table (model) and fields (features) you wish to include in your REST API. |
| 51 | + |
| 52 | +### Create a View |
| 53 | +- Use the standard Django REST framework to create your Django view. Django REST framework allows you to interact with your API in both JSON and a preset interactive template. If you feel like going the extra mile, make your database queryable to gather the information you need. |
| 54 | + |
| 55 | +### Designating a URL |
| 56 | +- Finally, designate url addresses where your page views can be found. Make sure to create a URL scheme that makes sense to how the intended user will interact with your API. |
| 57 | + |
| 58 | +### Running your Server |
| 59 | +- At this point it is time to test your API. This can be accomplished by the manage.py runserver command. Django's default location is localhost:8000/. From there, follow the naming scheme you created in your urls. Feel free to play with your API by using those filterable features you created! |
| 60 | + |
| 61 | + |
| 62 | +## Useful Weblinks |
| 63 | + |
| 64 | + |
| 65 | +- Django Startup and Features |
| 66 | + |
| 67 | + https://docs.djangoproject.com/en/2.2/intro/tutorial01/ |
| 68 | + |
| 69 | + https://docs.djangoproject.com/en/2.2/ref/applications/ |
| 70 | + |
| 71 | +- Django Models |
| 72 | + |
| 73 | + https://docs.djangoproject.com/en/2.2/ref/models/fields/ |
| 74 | + |
| 75 | + https://docs.djangoproject.com/en/2.2/topics/db/models/#automatic-primary-key-fields |
| 76 | + |
| 77 | +- Django REST Framework |
| 78 | + |
| 79 | + https://www.django-rest-framework.org/#installation |
| 80 | + |
| 81 | +- Serialization |
| 82 | + |
| 83 | + https://www.django-rest-framework.org/api-guide/serializers/#modelserializer |
| 84 | + |
| 85 | + https://www.django-rest-framework.org/api-guide/serializers/#specifying-read-only-fields |
| 86 | + |
| 87 | +- Views and URLS |
| 88 | + |
| 89 | + https://www.django-rest-framework.org/tutorial/quickstart/#views |
| 90 | + |
| 91 | + https://www.django-rest-framework.org/tutorial/quickstart/#urls |
0 commit comments