Mount a directory of markdown files as a sqlite virtual table in Django
Project description
Markdown files as a SQLite virtual table
This library provides a sqlite virtual table implementation that allows mounting a directory of markdown files as a sqlite table. This allows a user to take their markdown files from their favorite static site blog or note taking system, and access them from Django as a sqlite table.
This initial release only supports read-only operations (SELECT) though a future version will also support write operations (INSERT, UPDATE, DELETE)
Currently only supports sdist via PyPI, though a future version will attempt to support a few platforms/versions as wheels.
Usage
Add database connection to your settings.py
DATABASES = {
"default": {
# Configure our database engine to this library
"ENGINE": "markdowndb",
# This is still a real sqlite database so your other models
# can live here as usual
"NAME": BASE_DIR / "db.sqlite3",
# Optionally override the sqlite binary for `django-admin dbshell`
# since the builtin MacOX sqlite does not allow loading modules
"CLIENT": "/usr/local/opt/sqlite/bin/sqlite3",
# Root path to our markdown files. Our models will ultimately be
# built as ROOT / <model_label>
"ROOT": BASE_DIR / "content"
}
}
When creating models, add required_db_vendor = "markdowndb" and managed = False to your Meta class
from markdowndb.mixins import FrontmatterModel
class MyNotes(FrontmatterModel)
...
class Meta:
required_db_vendor = "markdowndb"
managed = False
The FrontmatterModel mixin provides access to many underlying fields by default.
See the comments on the model for specific fields.
Custom lookups
Our FrontmatterModel provides a metadata JSONField that we can use to get the entire frontmatter, but we can also configure a custom lookup.
Assuming frontmatter like the following
---
title: Some title
date: 2025-01-01 01:02:03
tags:
- one
- two
---
An excerpt for here.
<!--more-->
The longer blog post here.
We can define a custom model
from markdowndb.mixins import FrontmatterModel
class MyNotes(FrontmatterModel)
title = models.CharField()
date = models.DatetimeField()
class Meta:
required_db_vendor = "markdowndb"
managed = False
We have various ways of accessing our model
# Lets get our first note as a quick test
note = MyNotes.objects.first()
# By default, our primary key will match the file inode since that should be
# a constant indicator
note.pk == note.inode
# Calling the attribute directly returns 'Some title'
note.title
# As does getting the attribute from the metadata
note.metadata['title']
# Getting our post date lets Django process as a datetime value
note.date
# Though we can also pull the original string directly without conversion
note.metadata['date']
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django_markdown_database-0.1.1.tar.gz.
File metadata
- Download URL: django_markdown_database-0.1.1.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fe3c4325c5e6aa62e5dfe2ccda2e13c6c1aeac4d2456e56481b84a964904607
|
|
| MD5 |
589731768fe4603e0eeb1ddfff5478ea
|
|
| BLAKE2b-256 |
c24356ce83f3ed8f412ff1da2ec7fffe92a7b826f1dfa95609a5ab8408a1e131
|