Finance API Reference
Technical documentation of the models, views, and services in the Finance application.
finance.models
Models for the finance application.
Defines core financial entities such as Categories, Transactions, Budgets, and Savings Goals. Includes logic for tracking spending relative to budget limits.
Budget
Bases: Model
Defines a user's total spending allowance for a specific month.
Attributes:
| Name | Type | Description |
|---|---|---|
user |
ForeignKey
|
The budget owner. |
name |
CharField
|
Descriptive name (e.g., 'May 2026 Budget'). |
month |
int
|
1-12. |
year |
int
|
YYYY. |
total_limit |
Decimal
|
The maximum total spending allowed. |
status |
str
|
Current standing (Active, Exceeded, Completed). |
Source code in finance/models.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
spent
property
Calculate total expenses for this budget's month and year.
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Sum of all expenses in the matching period. |
Meta
__str__()
update_status()
Evaluate and update the budget status based on current spending.
Compares total spent against total_limit and persists the status field.
Source code in finance/models.py
BudgetCategoryLimit
Bases: Model
Sets a specific spending cap for a single category within a larger budget.
Attributes:
| Name | Type | Description |
|---|---|---|
budget |
ForeignKey
|
Parent budget. |
category |
ForeignKey
|
The category to limit. |
limit |
Decimal
|
The capped amount for this category. |
spent |
Decimal
|
Running total of expenses in this category. |
status |
str
|
Standing (Active, Close, Exceeded). |
Source code in finance/models.py
remaining
property
Calculate available funds remaining for this category.
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Difference between limit and spent. |
Meta
Metadata for the BudgetCategoryLimit model.
Source code in finance/models.py
add_spent(amount)
Increase the spent tally and refresh the status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
Decimal
|
Value to add. |
required |
update_status()
Evaluate status based on consumption percentage.
'Close' status is triggered at 90% consumption.
Source code in finance/models.py
Category
Bases: Model
Represents a classification for transactions (e.g., Food, Salary, Rent).
Categories can be system-predefined or custom-created by users.
Attributes:
| Name | Type | Description |
|---|---|---|
user |
ForeignKey
|
The user who created the category (None for predefined). |
name |
CharField
|
The display name of the category. |
type |
CharField
|
Choice of 'expense' or 'income'. |
is_predefined |
BooleanField
|
True if the category is available to all users. |
parent |
ForeignKey
|
Optional self-referential link for sub-categories. |
created_at |
DateTimeField
|
When the category was added. |
Source code in finance/models.py
Meta
SavingsGoal
Bases: Model
Defines a long-term savings target.
Attributes:
| Name | Type | Description |
|---|---|---|
user |
ForeignKey
|
Goal owner. |
name |
CharField
|
Name of the goal (e.g., 'New Car'). |
target_amount |
Decimal
|
Total money required. |
current_amount |
Decimal
|
Money saved so far. |
deadline |
DateField
|
Target date for completion. |
completed |
BooleanField
|
Completion status. |
Source code in finance/models.py
progress
property
Calculate the percentage towards the goal.
Returns:
| Name | Type | Description |
|---|---|---|
int |
Progress percentage (0-100). |
Meta
add_contribution(amount)
Log a contribution and check for goal completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
Decimal
|
Contribution value. |
required |
Source code in finance/models.py
Transaction
Bases: Model
Represents an individual financial movement (income or expense).
Attributes:
| Name | Type | Description |
|---|---|---|
user |
ForeignKey
|
The user who owns the transaction. |
type |
CharField
|
Choice of 'expense' or 'income'. |
category |
ForeignKey
|
The category assigned to the transaction. |
amount |
DecimalField
|
The monetary value. |
date |
DateField
|
When the transaction occurred. |
description |
CharField
|
A brief overview. |
notes |
TextField
|
Optional detailed commentary. |
source |
CharField
|
The origin of income (e.g., 'Employer Name'). |
created_at |
DateTimeField
|
Audit timestamp. |
Source code in finance/models.py
Meta
finance.serializers
Serializers for the finance application.
Handles validation and data conversion for categories, transactions, budgets, and savings goals. Includes business logic for linking transactions to the current user and service layer.
BudgetCategoryLimitSerializer
Bases: ModelSerializer
Serializer for assigning limits to specific categories within a budget.
Source code in finance/serializers.py
Meta
Metadata for BudgetCategoryLimitSerializer.
Source code in finance/serializers.py
BudgetSerializer
Bases: ModelSerializer
Serializer for monthly budgets.
Computes real-time 'spent' and 'remaining' values based on transaction history.
Source code in finance/serializers.py
Meta
Metadata for BudgetSerializer.
Source code in finance/serializers.py
create(validated_data)
Establish a new budget for the authenticated user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validated_data
|
dict
|
Validated input data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Budget |
Newly created budget instance. |
Source code in finance/serializers.py
get_remaining(obj)
Calculate available budget balance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Budget
|
The budget instance. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
remaining amount (total_limit - spent). |
Source code in finance/serializers.py
CategorySerializer
Bases: ModelSerializer
Serializer for expense and income categories.
Automatically handles user assignment and category type identification.
Source code in finance/serializers.py
Meta
create(validated_data)
Create a new custom category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validated_data
|
dict
|
Validated input data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Category |
Created category instance with authenticated user attached. |
Source code in finance/serializers.py
SavingsGoalSerializer
Bases: ModelSerializer
Serializer for user-defined savings targets.
Computes the 'progress' percentage based on target vs current amounts.
Source code in finance/serializers.py
Meta
Metadata for SavingsGoalSerializer.
Source code in finance/serializers.py
create(validated_data)
Register a new savings goal for the current user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validated_data
|
dict
|
Validated input data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SavingsGoal |
Created instance. |
Source code in finance/serializers.py
TransactionSerializer
Bases: ModelSerializer
Serializer for logging financial transactions.
Includes validation for amounts, category matching, and income requirements.
Source code in finance/serializers.py
Meta
Metadata for TransactionSerializer.
Source code in finance/serializers.py
create(validated_data)
Create a transaction via the TransactionService.
Delegates to the service layer to handle side effects like budget limit updates and notification triggers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validated_data
|
dict
|
Validated input data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Transaction |
The created transaction instance. |
Source code in finance/serializers.py
validate(data)
Perform complex cross-field validation.
Checks: 1. Amount is positive. 2. Transaction type matches the chosen category's type. 3. Income transactions have a source defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Input data to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Validated data. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If any business rule is violated. |
Source code in finance/serializers.py
finance.views
Views for managing financial entities.
Provides comprehensive API endpoints for: - Categories (predefined and custom) - Transactions (with filtering and summaries) - Budgets and per-category spending limits - Savings goals with contribution tracking - Financial reports (monthly and by category)
BudgetCategoryLimitViewSet
Bases: OwnerMixin, ModelViewSet
ViewSet for managing granular spending limits within a budget.
Allows users to assign specific portions of their total budget to individual categories.
Source code in finance/views.py
get_queryset()
Filter limits based on the owner of the parent budget.
Returns:
| Name | Type | Description |
|---|---|---|
QuerySet |
Category limits belonging to the user. |
BudgetViewSet
Bases: OwnerMixin, ModelViewSet
ViewSet for managing overall monthly budgets.
Allows setting the total spending limit for a specific month and year.
Source code in finance/views.py
CategoryViewSet
Bases: OwnerMixin, ModelViewSet
ViewSet for managing transaction categories.
Allows listing all categories (including global predefined ones) and creating/editing custom user-specific categories.
Source code in finance/views.py
get_queryset()
Return user-specific categories combined with system-wide predefined categories.
Returns:
| Name | Type | Description |
|---|---|---|
QuerySet |
Categories matching the criteria. |
Source code in finance/views.py
perform_create(serializer)
Assign the authenticated user to newly created categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serializer
|
CategorySerializer
|
The category serializer. |
required |
OwnerMixin
Mixin to restrict queryset access to the authenticated user's own data.
Ensures that users can only view, update, or delete objects they created.
Source code in finance/views.py
get_queryset()
Filter the base queryset to only include items belonging to the current user.
Returns:
| Name | Type | Description |
|---|---|---|
QuerySet |
Filtered queryset. |
ReportViewSet
Bases: ViewSet
ViewSet for high-level financial reports.
Provides aggregated analytics about income, expenses, and category distributions.
Source code in finance/views.py
by_category(request)
Generate a breakdown of spending by category for a period.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
List of categories with total amounts spent in each. |
Source code in finance/views.py
list(request)
monthly(request)
Generate a monthly financial summary.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Aggregated monthly data (Income, Expense, Balance). |
Source code in finance/views.py
SavingsGoalViewSet
Bases: OwnerMixin, ModelViewSet
ViewSet for managing long-term savings goals.
Provides a custom 'contribute' action to add money to a goal.
Source code in finance/views.py
contribute(request, pk=None)
Add funds to a specific savings goal.
Expected Data
amount (Decimal): The contribution value.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
The updated goal instance. |
Source code in finance/views.py
TransactionViewSet
Bases: OwnerMixin, ModelViewSet
ViewSet for logging and viewing financial transactions.
Supports filtering by type, category, and date range.
Source code in finance/views.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
by_category(request)
Aggregate spending by category.
Returns a list of categories and the total amount spent in each.
Source code in finance/views.py
get_queryset()
Apply optional filters based on query parameters.
Filters: - type: 'expense' or 'income' - category: ID of the category - date_from: Start date (YYYY-MM-DD) - date_to: End date (YYYY-MM-DD)
Returns:
| Name | Type | Description |
|---|---|---|
QuerySet |
Filtered transaction list. |
Source code in finance/views.py
summary(request)
Calculate the aggregate financial balance.
Query Parameters
month (int): Filter by month. year (int): Filter by year.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Object containing totals for income, expense, and net balance. |
Source code in finance/views.py
finance.services
Services for the finance application.
Contains the business logic for processing transactions, managing budgets, and generating reports. This layer abstracts complex logic away from views and models to maintain clean architecture.
BudgetService
Service for linking expense transactions to budgets and updating spending totals.
Handles the side effects of transaction creation on budget limits.
Source code in finance/services.py
get_budget_for_transaction(transaction)
Return the budget for the same month/year as the transaction, if one exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transaction
|
Transaction
|
The transaction instance. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Budget |
The matching budget instance or None. |
Source code in finance/services.py
process_expense(transaction)
Update the spent amount on the relevant budget category limit.
Also triggers a recalculation of the overall budget status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transaction
|
Transaction
|
The expense transaction being processed. |
required |
Source code in finance/services.py
ReportService
Service for generating financial reports aggregated by period or category.
Source code in finance/services.py
category_summary(user, month=None, year=None)
Return spending totals grouped by category for the given period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
The user whose summary to generate. |
required |
month
|
int
|
Month filter. |
None
|
year
|
int
|
Year filter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
List of dictionaries with 'category_id', 'category_name', and 'total'. |
Source code in finance/services.py
monthly_summary(user, month=None, year=None)
Return total income, total expense, and net balance for the given period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
The user whose summary to generate. |
required |
month
|
int
|
Month filter. |
None
|
year
|
int
|
Year filter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Summary data containing 'income', 'expense', and 'balance'. |
Source code in finance/services.py
TransactionService
Service for creating transactions and triggering post-creation side effects.
Acts as the entry point for transaction creation logic.
Source code in finance/services.py
create_transaction(user, validated_data)
Create a transaction and process it against the budget if it's an expense.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
The user creating the transaction. |
required |
validated_data
|
dict
|
Validated input data from the serializer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Transaction |
The newly created transaction. |