Invoice Management API

This Django Rest Framework application provides endpoints to manage invoices and their associated details.

Installation

  1. Clone the repository:

    git clone https://github.com/your_username/invoice-management-api.git cd invoice-management-api
  2. Create a virtual environment and activate it:

    python -m venv venv # For Windows: venv\Scripts\activate # For Unix or MacOS: source venv/bin/activate
  3. Install the dependencies:

    pip install -r requirements.txt
  4. Run database migrations:

    python manage.py migrate
  5. Run the development server:

    python manage.py runserver

API Endpoints

Invoices

List/Create Invoices

  • URL: /invoices/
  • HTTP Methods: GET, POST
  • Description: Endpoint to list all invoices or create a new invoice.
  • Payload Example (POST):
{ "date": "2023-12-18", "customer_name": "Test Customer", "details": [ { "description": "Test Description", "quantity": 5, "unit_price": 10, "price": 50 } // Add more details if needed ] }

Retrieve/Update/Delete Invoice

  • URL: /invoices/<int:pk>/
  • HTTP Methods: GET, PUT, DELETE
  • Description: Endpoint to retrieve, update, or delete a specific invoice.

Tests

The project includes comprehensive tests to ensure the functionality of the API endpoints. To run tests:

python manage.py test your_app_name

Implementation Details

Models

The application consists of two Django models:

  • Invoice: Fields include Date and Customer Name.
  • InvoiceDetail: Fields include Invoice (ForeignKey), Description, Quantity, Unit Price, and Price.

Serializers

Serializer classes are defined to transform model instances into JSON representations and vice versa.

  • InvoiceSerializer: Handles serialization and deserialization of Invoice objects.
  • InvoiceDetailSerializer: Handles serialization and deserialization of InvoiceDetail objects.

Views

Views are implemented using Django Rest Framework's generic views:

  • InvoiceListCreateView: Handles listing and creation of Invoice instances.
  • InvoiceDetailView: Handles retrieval, update, and deletion of specific Invoice instances.

URLs

URL patterns are defined to map views to specific endpoints:

  • /invoices/: Maps to InvoiceListCreateView for listing and creation of invoices.
  • /invoices/<int:pk>/: Maps to InvoiceDetailView for retrieval, update, and deletion of specific invoices.