This content originally appeared on DEV Community and was authored by Joe ???????
To track finances, we need two main data types—categories (like "Food") and transactions (money in or out).
1. Plan Your Models
- Category: e.g. Food, Rent, Salary
- Transaction: amount, date, category, type, notes
2. Code the Models
Edit tracker/models.py:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.name
class Transaction(models.Model):
INCOME = 'IN'
EXPENSE = 'EX'
TRANSACTION_TYPES = [
(INCOME, 'Income'),
(EXPENSE, 'Expense'),
]
category = models.ForeignKey(Category, on_delete=models.CASCADE)
amount = models.DecimalField(max_digits=10, decimal_places=2)
date = models.DateField()
transaction_type = models.CharField(max_length=2, choices=TRANSACTION_TYPES)
notes = models.TextField(blank=True)
def __str__(self):
return f"{self.get_transaction_type_display()} - {self.category.name}: {self.amount} on {self.date}"
3. Why This Structure?
- ForeignKey connects transactions to categories
- Choices enforce transaction type
Let’s turn these models into database tables next!
This content originally appeared on DEV Community and was authored by Joe ???????
Joe ??????? | Sciencx (2025-09-11T21:58:27+00:00) Designing Django Models: Transactions & Categories for Finance Apps. Retrieved from https://www.scien.cx/2025/09/11/designing-django-models-transactions-categories-for-finance-apps/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.