This project is a Django REST Framework API for managing a collection of books and authors. It includes features like token-based authentication, filtering, searching, and ordering.
- Book and Author API Endpoints: Provides full CRUD (Create, Read, Update, Delete) functionality for books and authors.
- Token Authentication: Secures the API using
djangorestframework.authtoken. - Filtering: Allows filtering books by
publication_yearandauthor__name. - Searching: Enables searching for books by
titleandauthor__name. - Ordering: Supports ordering books by
titleandpublication_year. - Serialization: Uses
ModelSerializerto handle the conversion ofBookandAuthormodels to and from JSON. - Nested Serialization: The
Authorserializer includes a list of books written by the author.
To run this project, you need to have Python, Django, and Django REST Framework installed.
-
Clone the repository or download the project files.
-
Navigate to the project directory:
cd advanced-api-project -
Install the dependencies:
pip install -r requirements.txt
-
Apply the database migrations:
python manage.py migrate
-
Create a superuser:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
The following API endpoints are available:
POST /api/token/: Obtain an authentication token by providing a username and password.GET /api/books/: Get a list of all books.POST /api/books/create: Create a new book (requires authentication).GET /api/books/<int:pk>/: Retrieve a specific book by its ID.PUT /api/books/update/<int:pk>/: Update a book (requires authentication).DELETE /api/books/delete/<int:pk>/: Delete a book (requires authentication).GET /api/authors/: Get a list of all authors and their books.POST /api/authors/: Create a new author (requires authentication).
-
Get an authentication token:
curl -X POST http://127.0.0.1:8000/api/token/ -d "username=your_username&password=your_password" -
Create a new book:
curl -X POST http://127.0.0.1:8000/api/books/create -H "Authorization: Token YOUR_TOKEN_HERE" -d "title=New Book&author=1&publication_year=2023"
-
List all books:
curl http://127.0.0.1:8000/api/books/