Add categories

This commit is contained in:
Edgar P. Burkhart 2022-05-20 13:44:46 +02:00
parent 3f73232312
commit 36be624f5c
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 49 additions and 2 deletions

View file

@ -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)

View file

@ -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'),
),
]

View file

@ -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):