in blog | Matthias Kestenholz: Posts about Django |
---|---|
original entry | Weeknotes (2024 week 18) |
We have a student helping out with adding async support to the Django Debug Toolbar. It’s great that someone can spend some concentrated time to work on this. Tim and others have done all the necessary preparation work, I’m only helping from the sidelines so don’t thank me.
Two teams from my company are participating in the Bike to Work Challenge 2024. It’s what I do anyway (if I’m not working from home) but maybe it helps build others some motivation to get on the bicycle once more. Public transports in the city where I live are great but I’ll always take the bike when I can. I also went on my first mountain bike ride in a few months yesterday, good fun.
The django-json-schema-editor has gained support for referencing Django models. Here’s an example schema excerpt:
{
...
"articles": {
"type": "array",
"format": "table",
"title": _("articles"),
"minItems": 1,
"maxItems": 3,
"items": {
"type": "string",
"title": _("article"),
"format": "foreign_key",
"options": {
"url": "/admin/articles/article/?_popup=1&_to_field=id",
},
},
},
...
}
The ID field is stringly typed; using an integer directly wouldn’t work because the empty string isn’t a valid integer.
The problem with referencing models in this way is that there’s no way to know
if the referenced object is still around or not, or even to protect it against
deletion. The bundled django-content-editor JSONPlugin
now supports
automatically generating a ManyToManyField
with a through
model which
protects articles from deletion as long as they are referenced from a
JSONPlugin
instance. The register_reference
line creates the mentioned
model with an on_delete=models.PROTECT
foreign key to articles and a
post_save
handler which updates said references.
from django_json_schema_editor.plugins import JSONPluginBase, register_reference
from articles.models import Article
class JSONPlugin(JSONPluginBase, ...):
pass
register_reference(JSONPlugin, "articles", Article)
extra_context
argument to our changelist_view
implementation.