Fixed nested style preserving issue on TOMLFied

This commit is contained in:
Oracle 2025-08-27 06:11:49 +02:00
parent 2faf5e29ef
commit c2e89d48d5
2 changed files with 13 additions and 2 deletions

View File

@ -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

View File

@ -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')