Skip to main content

Flat-file model instances for Django

Project description

https://img.shields.io/pypi/v/yamdl.svg https://img.shields.io/pypi/l/yamdl.svg

Lets you store instances of Django models as flat files (simplified fixtures). For when you want to store content in a Git repo, but still want to be able to use the normal Django ORM methods and shortcut functions.

It works by loading the data into an in-memory SQLite database on startup, and then serving queries from there. This means it adds a little time to your app’s boot, but versus static files, it still lets you write queries and have dynamic views, while all being incredibly fast as queries return in microseconds.

It does not persist changes to the models back into files - this is purely for authoring content in a text editor and using it via Django.

Why not use normal fixtures?

They’re not only a little verbose, but they need to be loaded into a non-memory database (slower) and you need lots of logic to work out if you should update or delete existing entries.

Installation

First, install the package:

pip install yamdl

Then, add it to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'yamdl',
    ...
]

Then, add the in-memory database to DATABASES:

DATABASES = {
    ...
    'yamdl': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'file:yamdl-db?mode=memory&cache=shared',
    }
}

Then, add a YAMDL_DIRECTORIES setting which defines where your directories of YAML files can be found (it’s a list):

YAMDL_DIRECTORIES = [
    os.path.join(PROJECT_DIR, "content"),
]

Finally, add the database router:

DATABASE_ROUTERS = [
    "yamdl.router.YamdlRouter",
]

Usage

First, add the __yamdl__ attribute to the models you want to use static content. A model can only be static or dynamic, not both:

class MyModel(models.Model):
    ...
    __yamdl__ = True

Then, start making static files under one of the directories you listed in the YAMDL_DIRECTORIES setting above. Within one of these, make a directory with the format appname.modelname, and then YAML files ending in .yaml:

andrew-site/
    content/
        speaking.Talk/
            2017.yaml
            2016.yaml

Within those YAML files, you can define either a list of model instances, like this:

- title: 'Alabama'
  section: us-states

- title: 'Alaska'
  section: us-states
  done: 2016-11-18
  place_name: Fairbanks

- title: 'Arizona'
  section: us-states
  done: 2016-05-20
  place_name: Flagstaff

Or a single model instance at the top level, like this:

conference: DjangoCon AU
title: Horrors of Distributed Systems
when: 2017-08-04
description: Stepping through some of the myriad ways in which systems can fail that programmers don't expect, and how this hostile environment affects the design of distributed systems.
city: Melbourne
country: AU
slides_url: https://speakerdeck.com/andrewgodwin/horrors-of-distributed-systems
video_url: https://www.youtube.com/watch?v=jx1Hkxe64Xs

You can also define a Markdown document (ending in .md) below a document separator, and it will be loaded into the column called content:

date: 2022-01-18 21:00:00+00:00
image: blog/2022/241.jpg
image_expand: true
section: van-build
slug: planning-a-van
title: Planning A Van

---

What's In A Van?
----------------

So, I have decided to embark on my biggest project to date (and probably for a while, unless I finally get somewhere to build a cabin) - building myself a camper van, from scratch. Well, from an empty cargo van, anyway.

Files can be nested at any level under their model directory, so you can group the files together in directories (for example, blog posts by year) if you want.

The files are also added to the Django autoreloader, so if you are using the development server, it will reload as you edit the files (so you can see changes reflected live - they are only read on server start).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yamdl-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

yamdl-1.0.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file yamdl-1.0.0.tar.gz.

File metadata

  • Download URL: yamdl-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for yamdl-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9096da8f72ffda814686eab37ab1be1c9802faf1b01f9351bdad7c78a938fb74
MD5 4e27c1bab06f2d0ba6588652a6910426
BLAKE2b-256 7277aa2d66e28cd68c8c5837f1fcc23e093d049121d6ed3d20474e94591efa57

See more details on using hashes here.

File details

Details for the file yamdl-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: yamdl-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for yamdl-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 107b3761b3b3d14f054f9f1aa8d1ffdfb8bdd2b993e275e7cefff525bf7b00bc
MD5 fe6fbc3a4129c8a796eeea8b8669e90d
BLAKE2b-256 d4be4637ef482a3b01e0e5016b5db53aaea0760c77fe79b7078024fcf8a83e50

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page