A friend recently wrote “our personal database , about how they maintain a postgres database for various projects. I wrote something similar when I wrote about my personal api , though Rubenerd’s post made me want to write a little bit more about it.
Since I am already using django for various projects, my personal API is also built on Django. While I could modify thing directly using something like pgAdmin, building on Django means I can take advantage of the models and admin built in to Django.
Previously I would create a new app using the built in startapp command.
Note: Django calls its reusable modules ‘apps’ and somewhat predates the usage of ‘webapps’
django-admin startapp <name>
Though now that I am more experienced with Django, I will typically just type everything manually. First, I’ll typically create a model to hold the data I want.
# <project>/<appname>/models.py
from django.db import models
class MyModel:
name = models.CharField(max_length=128)
info = models.TextField()
Then ensure that is added to INSTALLED_APPS
as project_name.app_name
.
Next, I will hook it up to the Django admin so that I can edit things.
# <project>/<appname>/admin.py
from django.contrib import admin
from . import models
@admin.register(models.MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ("name",)
With just a model and admin file (make sure you also have __init__.py
so it is recongized as a Python package), I have something similar to a customized pgAdmin but for my specific data model.
When you start customizing things more, such as adding enumeration type or foreign key to another object, the Django Admin keeps your personalized admin up to date.
Later if requirements change even more, one can create custom edit views or detailed views to customize how things are rendered.
I am currently using this pattern for a few small apps that I use for maintaining my home.
- homeutil - Each time I pay a Gas/Electric/Water bill, I add an entry and I can graph over time.
- pantry - A prototype that I am using to keep track of bags of bulk items like Cat Food
- calendar - Keeps track of some events in a django database, then I use a custom view to generate an ical feed
I also have some other apps I have since split out into their own packages (though I still need to document and publish them).
I imagine that other frameworks like Rails would make it easy to do something similar though Django is the only framework I have significant experience with.