Truly learning to build an application by doing

Django forms wer nebulous to me until I had to build one for two models at once

by Carl James | BLOOMINGTON, IN | Dec. 29, 2023

Screenshot of application output during development

Nothing helps a person learn to build applications better than actually doing it. While I love the training resources out there, just following them step-by-step is not enough to even learn basic concepts. Only in the process of building an application with more complex data am I able to get a much better core understanding of what the Python/Django web framework operates.

Forms

The training examples I have seen are all of a single model being rendered as a form. Most of the training also has this done with built-in Django classes. While this is functional and easy to type, it didn't help to learn what a form truly was. My initial mental image was a reflection of the data model.

I ran into a new challenge that wasn't solved by this thinking. I wanted to be able to update two different models with a single click. At first I thought about using a cascade of HTMX calls on the front end to send multiple form posts. That introduced another problem, because although the two model objects I wanted to create were different, they are also related by a foreign key.

After stepping away and coming back, I looked at the code:

  1. The form
  2. The views
  3. The model
  4. The template

It then dawned on me. The form does not need to be limited to the structure of a single model. It can fill and submit any data I want it to. Then I can use that data to create multiple model objects, which is a Django way of saying rows in the database.

The view code for making two different related model objects

More importantly I can then use the .last() syntax to pull the primary key one model object to automatically add it as a foreign key the other object. This is all done with one form and one submission.

While yes it works, what it really did was make it more conceptually clear to me what a form in Django really is. It is a Python contruct that renders input fields in HTML that create a data object to be sent the server. Once the server has the data, the server can do anything it is programmed to do with that data. While forms are often just mirrors of data models, they don't have to be. Even though I've done Python web work on the side for over five years and Django for three years, I never really understood this concept until today.

I'm calling that a big win.

I have more I want to do with my application. Most of the current functionality is covered by my suite of 15 tests. My goals include:

  1. setting up multiple users (pre-assigned)
  2. pre-populating the most recent data in the scoreboard automatically
  3. improving the CSS more
  4. adding featured images and user icons

I will come back periodically to update on the application development process.


Carl James is CompTIA A+ ce Certified

Categories || All || Coding


See a complete list of all jovian34 blogs

As always, I'd love to hear you're thoughts and questions, so feel free to shoot a Noot to @jovian34 on Mastodon and I will gladly respond and perhaps write a blog post on a topic you suggest.