From c2e89d48d59dc8685a4f2cb0f02f01d0c3d8a8f7 Mon Sep 17 00:00:00 2001 From: Oracle Date: Wed, 27 Aug 2025 06:11:49 +0200 Subject: [PATCH] Fixed nested style preserving issue on TOMLFied --- asyncron/fields/toml.py | 6 +++++- asyncron/utils.py | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/asyncron/fields/toml.py b/asyncron/fields/toml.py index fd18afd..44e0561 100644 --- a/asyncron/fields/toml.py +++ b/asyncron/fields/toml.py @@ -2,8 +2,11 @@ from django.db import models from django.forms import fields from asyncron.widgets import Codearea +from asyncron.utils import rupdate import tomlkit + + from tomlkit.exceptions import ParseError from tomlkit.toml_document import TOMLDocument @@ -72,7 +75,8 @@ class TOMLField( models.JSONField ): # try: value_as_toml = tomlkit.loads( value_as_dict.pop('_toml', '') ) except: value_as_toml = tomlkit.document() - value_as_toml.update( value_as_dict ) + #We can't just use "update", we have to deep_update or we'll loose any non-shallow comments / styling + rupdate( value_as_toml, value_as_dict ) return value_as_toml diff --git a/asyncron/utils.py b/asyncron/utils.py index c4bf9ba..ed6cc4d 100644 --- a/asyncron/utils.py +++ b/asyncron/utils.py @@ -9,7 +9,14 @@ def rgetattr(obj, attr, *args): return getattr(obj, attr, *args) return functools.reduce(_getattr, [obj] + attr.split('.')) - +import collections.abc +def rupdate(d, u): #https://stackoverflow.com/a/3233356/ + for k, v in u.items(): + if isinstance(v, collections.abc.Mapping): + d[k] = rupdate(d.get(k, {}), v) + else: + d[k] = v + return d #Django keeps giving: exception=OperationalError('the connection is closed')