Finance Project 6: Managing the data

I have a basic data model, so now I need a way to enter data.

Django has a basic admin interface that can be used for any object in the model. The interface provides basic CRUD – Create/Read/Update/Delete – operations, and is a handy way to get started.

Using it is as simple as registering the objects in the admin.py file:

from django.contrib import admin
from .models import Account, AccountType, AccountCategory, Transaction, Entry

admin.site.register(Account)
admin.site.register(AccountType)
admin.site.register(AccountCategory)
admin.site.register(Transaction)
admin.site.register(Entry) 

It’s a very simple interface, but it can be handy for setting up some basic data. I’ve used it to create the account types.

I can also use it to set up the account categories. As you can see, it provides a drop down list for the account types using the foreign key defined in the object model:

The admin interface is no substitute for a full user interface, but it’s a handy way to get started and manage data.

Building a real interface requires Django views, and it’s quite simple to set up a list view. A really basic list of account types needs three things.

First of all, a URL mapping in finance/urls.py:

from django.urls import path
from . import views
app_name = 'finance'
urlpatterns = [
    path('account_types/', views.account_type_list, name="account_type_list")
]

This defines the URL: /finance/account_types/ that will call the account_type_list view. The corresponding view, in finance/views.py is:

from django.shortcuts import render
from .models import AccountType

def account_type_list(request):
  list = AccountType.objects.order_by('name')
  context = {'account_type_list': list}
  
  return render(request, 'finance/account_type_list.html', context)

The view gathers data and then uses the account_type_list.html template to display it. The template file is /finance/templates/finance/account_type_list.html:

{% if account_type_list %}
<ul>
    {% for account_type in account_type_list %}
      <li><a href="/finance/account_type/{{ account_type_id }}/">{{ account_type.name }}</a></li>
    {% endfor %}
</ul>
{% else %}
<P>No account types defined</P>
{% endif %}

This is a simple list with each account type being a link to an, as yet undefined, URL which will be used to edit or delete the item.

Putting it all together renders a distinctly unimpressive webpage:

Time to start thinking about adding some CSS to get a proper GUI…

in Finance Project

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts