An example project demoing the use of a many-to-many model with django rest framework for creating an API capable of handling postal address.
Note: this is NOT the correct way to handle postal addresses. Postal address are best modeled by a many-to-one relationship, rather than the many-to-many written here. I've left the code as it is, as a demo for handle many-to-many relationships in django This project demos several aspects of django, specifically:
- Django Rest Framework
- Gitlab CI
- GithubCI
- SwaggerUI view
- User is able to create a new address
- User will not be able to add a duplicated address to their account
- User is able to retrieve all their postal addresses
- User is able to retrieve a large number of address entries in a practical way
- User is able to filter retrieved addresses using request parameters
- User is able to update existing addresses
- User is able to delete one
- User is able to delete multiple addresses
- User is able to authenticate with a username and a password
- User can log out
Additionally, added
- Enabled token Based authentication
- Added Swagger API webview
- A user can have a large number of addresses
- A user can have addresses from any country
- A user's client can hold state (if necessary for certain endpoints)
- You must ensure the application can be run by another engineer with access to the repository
- You do not need to deploy this code to a server
- You should document any assumptions you make about the product, especially where you would have a question about it
Note: Currently Features that are incomplete proper handling of Address Put, Bulk get pagination Get filtering
$ python3 -m venv venv; source venv/bin/activate; pip install -r requirements-dev.txt
$ python manage.py migrate
$ python manage.py runserver_plus
Don't use in production
$ python manage.py shell --command="from django.contrib.auth.models import User
User.objects.create_superuser('a', 'a', 'a')"
Relies on django extensions - won't run unless in devmode (i.e. won't work in prod)
$ python manage.py runscript generate_dummy_data
$ python manage.py test
these should also work in parallel
$ python manage.py test --parallel
Auto Lint using https://github.com/psf/black
$ black src
And check code quality using
$ pylint src
Its possible to install gitlab runner on your local machine and test the .gitlab-ci.yaml
$ gitlab-runner exec docker test\ django