diff --git a/nummi/main/admin.py b/nummi/main/admin.py index ea0111a..d8bde1e 100644 --- a/nummi/main/admin.py +++ b/nummi/main/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from .models import Transaction, Invoice +from .models import Transaction, Invoice, Category admin.site.register(Transaction) admin.site.register(Invoice) +admin.site.register(Category) diff --git a/nummi/main/migrations/0002_category_transaction_category.py b/nummi/main/migrations/0002_category_transaction_category.py new file mode 100644 index 0000000..be19f25 --- /dev/null +++ b/nummi/main/migrations/0002_category_transaction_category.py @@ -0,0 +1,28 @@ +# Generated by Django 4.0.4 on 2022-05-20 11:34 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), + ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category')), + ], + ), + migrations.AddField( + model_name='transaction', + name='category', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.category'), + ), + ] diff --git a/nummi/main/models.py b/nummi/main/models.py index a5f30f4..8e6a895 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -3,15 +3,33 @@ from django.db import models from django.forms import ModelForm +class Category(models.Model): + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=256) + parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True) + + def __str__(self): + if self.parent: + return f"{self.parent}>{self.name}" + else: + return f"{self.name}" + + class Transaction(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=256) description = models.TextField() value = models.DecimalField(max_digits=12, decimal_places=2) date = models.DateField() + category = models.ForeignKey( + Category, on_delete=models.SET_NULL, blank=True, null=True + ) def __str__(self): - return f"{self.date} {self.name}: {self.value}€" + res = f"{self.date} {self.name}: {self.value}€" + if self.category: + return f"{res} ({self.category})" + return res class TransactionForm(ModelForm):