Fischer, Part 2: Tagging
In Part 1, I learned how to do views and routes in nanodjango, got some basic templates set up, and vendored Pico CSS to make those pages look a little nicer. Tagging is going to be an important part of making this all work the way I want, and the easiest way to solve that is with django-taggit. I'm tackling this next so I can learn whether adding a third-party app means I need to go ahead and expand my little nanodjango project into a full, multi-file project already.
First, I obviously need to add django-taggit
to my poetry project.
poetry add django-taggit
Based on the nanodjango documentation, it looks like it then should be a simple matter of passing some extra settings ot the Django
object constructor when I set up my app.
app = Django(
EXTRA_APPS = ["taggit"],
TAGGIT_CASE_INSENSITIVE=True,
)
Then I need to add a tags
field to my Bookmark
model:
from taggit.managers import TaggableManager
@app.admin
class Bookmark(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="bookmarks"
)
title = models.CharField(max_length=200)
summary = models.TextField(blank=True)
url = models.URLField()
is_favorite = models.BooleanField(default=False)
notes = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
tags = TaggableManager()
def __str__(self):
return f"[{self.title}]({self.url})"
class Meta:
ordering = ["-created_at"]
Note that TaggableManager
can only be imported after the Django()
constructor is called. That surprised me initially.
With those changes, I started the server, which ran the migrations. When I visited the admin site, I saw the tags table, and I was able to add tags to individual bookmarks.
I also learned that adding a third party app does not mean the end of my ability to use nanodjango for this project.
If you want to follow along with my progress, you can get the code here.
Return to the introduction Read Part 3: Importing and Exporting Bookmarks