Skip to content

Commit

Permalink
create base models
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmurilo75 committed Mar 5, 2024
1 parent 704350b commit 100a1fb
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 7 deletions.
10 changes: 5 additions & 5 deletions development_notebook/2024-03-04.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The objective of today development session is to define basic models necessary f
We'll iterate as per hakibenita's advice:
1. Define the business requirements.
2. Write down a naive implementation and model definition.
3. Challenge the solution.
3. Challenge the solution.
4. Refine and repeat.

## Business Requirements
Expand All @@ -25,16 +25,16 @@ User <-1-N-> Account <-1-N-> Transactions
## Work Log

* Create core app from 'manage.py startapp'
* Added app to project

* Add app to project

Continued at 2024-03-05
* Define core models as above
* Create superuser
* Define model admins
* Define model admins
* Add data in model admins

* Commit changes

# To Do
[]. [[Deploy a Django Project to pythonanywhere Free-tier]]
[]. [[Configure Django from Cookiecutter to MySQL]]

17 changes: 16 additions & 1 deletion negligent_octopus/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
from django.contrib import admin

# Register your models here.
from .models import Account
from .models import Transaction


@admin.register(Account)
class AccountAdmin(admin.ModelAdmin):
list_display = ["name", "owner", "balance", "modified", "created", "is_removed"]
search_fields = ["name", "owner__username"]
list_filter = ["is_removed", "modified"]


@admin.register(Transaction)
class TransactionAdmin(admin.ModelAdmin):
list_display = ["title", "account", "get_account_owner", "date", "amount"]
search_fields = ["account__owner__username", "account__name", "title"]
list_filter = ["account__owner", "account", "date"]
109 changes: 109 additions & 0 deletions negligent_octopus/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Generated by Django 4.2.10 on 2024-03-05 12:24

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Account",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
model_utils.fields.AutoCreatedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="created",
),
),
(
"modified",
model_utils.fields.AutoLastModifiedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="modified",
),
),
("is_removed", models.BooleanField(default=False)),
("name", models.CharField(max_length=255)),
("balance", models.FloatField(default=0.0, editable=False)),
(
"owner",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"verbose_name": "Account",
"verbose_name_plural": "Accounts",
"ordering": ["-modified", "name"],
},
),
migrations.CreateModel(
name="Transaction",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
model_utils.fields.AutoCreatedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="created",
),
),
(
"modified",
model_utils.fields.AutoLastModifiedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="modified",
),
),
("amount", models.FloatField()),
("date", models.DateField(default=django.utils.timezone.now)),
("title", models.CharField(max_length=127)),
("description", models.TextField(blank=True)),
(
"account",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.account"
),
),
],
options={
"verbose_name": "Transaction",
"verbose_name_plural": "Transactions",
"ordering": ["date", "-modified"],
},
),
]
38 changes: 37 additions & 1 deletion negligent_octopus/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
from django.db import models
from django.utils import timezone
from model_utils.models import SoftDeletableModel
from model_utils.models import TimeStampedModel

# Create your models here.
from negligent_octopus.users.models import User


class Account(TimeStampedModel, SoftDeletableModel):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
balance = models.FloatField(default=0.0, editable=False)

def __str__(self):
return self.name

class Meta:
verbose_name = "Account"
verbose_name_plural = "Accounts"
ordering = ["-modified", "name"]


class Transaction(TimeStampedModel):
account = models.ForeignKey(Account, on_delete=models.CASCADE)
amount = models.FloatField()
date = models.DateField(default=timezone.now)
title = models.CharField(max_length=127)
description = models.TextField(blank=True)

def get_account_owner(self):
return str(self.account.owner)

def __str__(self):
return self.title

class Meta:
verbose_name = "Transaction"
verbose_name_plural = "Transactions"
ordering = ["date", "-modified"]

0 comments on commit 100a1fb

Please sign in to comment.