abelcastro.dev

My initial macOS setup - tools and resources list

2021-10-07

macosdockerdjango

Django and htmx

2021-08-27

htmxdjango

Create and use "dummy" Models in a Test Case in Django

2021-04-17

testingdjango
12
...
9
10
11

Abel Castro 2026 - checkout the source code of this page on GitHub - Privacy Policy

This is a collection of commands and resources that were useful for me with the initial setup of my MacBook.

  • Installs Xcode Command Line Tools
xcode-select --install
  • Generate SSH key and add it to the ssh agent
  • Homebrew https://brew.sh/
  • Oh my zsh https://ohmyz.sh/
  • nvm https://github.com/nvm-sh/nvm#git-install
  • Install Docker Desktop https://docs.docker.com/desktop/mac/install/
  • Rectangle: Move and resize windows in macOS using keyboard shortcuts or snap areas https://rectangleapp.com/
  • Add some shell aliases. I can't work without these:
alias g='git'
alias gc='git checkout'
alias gco='git commit'
alias gs='git status'
alias gp='git pull'

alias dc='docker-compose'
alias up='docker-compose up'
alias down='docker-compose down'

With htmx is possible to build dynamic Webapps without REST-APIs and JavaScript. Just simple Django views that returns html.

You can learn more about it in this interesting Django chat podcast episode with the htmx creator Carson Gross.

I really like the idea and I wanted since a while to try htmx working together with Django and finally, I managed to work on that. I created this repository with some implementations with Django of the code examples from the htmx docs page.

**Update: **also this website has some htmx magic on it. For example the post pagination or the search function are built with htmx.

Let's image we need a new model only for a test case and we don't really want to register in our project. We can create something similar than this:

example_app.tests.test_app.models.TestModel

from django.db import models


class TestModel(models.Model):
    field_a = models.IntegerField()
    field_b = models.IntegerField()

    class Meta:
        app_label = 'test_app'

We could try to use TestModel and create objects in a test case: test_models.py

from django.test import TestCase

from example_app.tests.test_app.models import TestModel


class TestOverridingInstalledApps(TestCase):
    def setUp(self):
        self.test_model = TestModel.objects.create(
            field_a=1,
            field_b=2,
        )

    def test_objects(self):
        self.assertEqual(TestModel.objects.count(), 1)

But if you run the tests like this, the test will fail and return something similar than that:

./manage.py test

django.db.utils.ProgrammingError: relation "test_app_testmodel" does not exist
LINE 1: INSERT INTO "test_app_testmodel" ("field_a", "field_b") VALU...

It fails because Django needs to have TestModel registered in INSTALLED_APPS but we don't really want to add our example_app.tests.test_app to INSTALLED_APPS because we only need it when we run the tests.

The solution is to to add the test_app to the settings with modify_settings and calling migrate.

test_models.py

from django.core.management import call_command
from django.test import TestCase, modify_settings

from example_app.tests.test_app.models import TestModel


@modify_settings(INSTALLED_APPS={
    'append': 'example_app.tests.test_app',
})
class TestOverridingInstalledApps(TestCase):
    def setUp(self):
        call_command('migrate', run_syncdb=True)
        self.test_model = TestModel.objects.create(
            field_a=1,
            field_b=2,
        )

    def test_objects(self):
        self.assertEqual(TestModel.objects.count(), 1)

You can see the complete source code here.