This project is a simple Django REST API that integrates MongoDB as the main database. It implements CRUD operations on a Pharmacies collection.
- MongoDB database to store pharmacy documents
- Django REST Framework to build the RESTful API
- djongo MongoDB adapter to connect Django and MongoDB
- Basic authentication for API endpoints
- API documentation using OpenAPI/Swagger
- API testing
- Python >= 3.8
- Django
- Django Rest Framework
- MongoDB
- Clone the repo
[email protected]:mustafa-kamel/pharmacies_api_mongodb.git
- Create a virtual environment
python -m venv venv
- Activate virtual environment
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Copy the
.env.example
file to.env
runcp .env.example .env
- Update the variables in
.env
file according to your configurations. - Apply migrations:
python manage.py migrate
- Create a superuser:
python manage.py createsuperuser
- Run the server:
python manage.py runserver
The project uses MongoDB as the backend database. Make sure MongoDB is installed locally and running on port 27017.
Create a local MongoDB database called pharmacies
from MongoDBCompass or from shell using:
mongosh
use pharmacies
If you want to use a cloud database instead you will need to add its configuration to the project settings file.
Make sure to set the following environment variables in .env
:
MONGO_DB
: The mongodb name.SECRET_KEY
: Django secret key.DEBUG
: Set toTrue
for development,False
for production.
- Use the Django admin interface or the API endpoint
/api/users/
to create a new user, Or you can do that simply from the django shell:
python manage.py shell
Then you need to create a new user using this code:
from django.contrib.auth.models import User
User.objects.create_user(username="USERNAME", password="USER_PASSWORD")
These user credentials will be used for basic authentication.
- Use the Django development server to run the API:
python manage.py runserver
- Access the API at
http://localhost:8000/pharmacies/
- Use pytest to run tests:
coverage run -m pytest
. - Generate coverage report:
coverage report
orcoverage html
for HTML report that shows a 99% test coverage.
- API documentation is available at http://localhost:8000/api/schema/swagger-ui/ or http://localhost:8000/api/schema/redoc/ once the server is running.
- Also check the project's Postman collection for a comprehensive API documentation, including request examples; also you can select the
pharmacies
environment and run the Postman collection to validate its functionality.