This repo shows the creation of REST APIs step by step. Due to its presentational purpose some of the endpoints does not have protection and every user can modify the data. Examples of such permissions are show in other endpoints (check the Permissions section). If you use this repo as a reference while building your project, make sure you have properly tested the permission to for each endpoint (test CRUD with anonymous user, a user who is the object owner and a user that is not the object owner)
Happy Coding )))
- adding README
- adding gitignore
- adding
docker-compose
for running PostgreSQL
- Adding
requirements.txt
- Django==2.1.1
- psycopg2==2.7.5
- Creating Django project
- django-admin startproject drf_workshop .
- Configuring DATABASES
- Run the project:
./manage.py runserver
- Add
djangorestframework==3.8.2
torequirements.txt
- Add
rest_framework
toINSTALLED_APPS
- Create the
api
app.python manage.py startapp api
- Create a router and add it to the urls
- Create
urls.py
inapi
- Create a new router and include its URLs
- Include the API urls in the general URLs config
- Test that it works
http://127.0.0.1:8000/api/
- Create
- Let's migrate first
python manage.py migrate
- Add the Users to the API
- create
users
folder inside theapi
app - add
serializer.py
to the users folder - create a signup serializer with 3 fields:
username
,email
,password
- create a signup view (APIView) that users the serializer
- install factory-boy
- create user factory (
users/factories.py
) - create test case for the view. Use https://gist.github.com/IlianIliev/4dd1d6b667f36be0e501321e195f6db6
- add the view to the url config
- add a test to verify the serialization
- create
- Create a login test an watch it fail
- Create a LoginView and add it to the url
- The test will fail (tricky moment wih the UserFactory)
- Fix the factory and watch the test succeed
- Lets talk about token auth (and JWT)
- Create Profile serializer
- Create profile view
- Write tests for getting the profile as logged and anonymous user
- Specify the permissions for the View
- Add test for profile update
- Create the authors app
python manage.py startapp authors
- add
authors
toINSTALLED_APPS
- Create the author models with the following fields:
name
,date_of_birth
,website
,bio
- Make the migrations
- Create the serializer
- Create the view (extends ModelViewSet)
- Register the view with the router and take a look at the browsable API
- Let's add some tests
- Settings pagination globally
- Fixing broken tests
- Let's add books app
- The book model has the following fields:
title
,authors
,description
- Creating the serializer without relation
- Adding the nested serializer
- Can we have link to the author - HyperlinkedModelSerializer
- Add test to create book
- Add test to update existing book
- Delete book
- Test that anonymous users can not delete books
- Set global permissions
- Add book ownership
- HiddenField + CurrentUserDefault
- Protect delete for owner only
- Let's create like functionality
- Creating "My books" endpoint