1- # Copyright The IETF Trust 2021, All Rights Reserved
2- # -*- coding: utf-8 -*-
3-
4- # Taken from/inspired by
5- # https://stackoverflow.com/questions/55147169/django-admin-jsonfield-default-empty-dict-wont-save-in-admin
6- #
7- # JSONField should recognize {}, (), and [] as valid, non-empty JSON
8- # values. However, the base Field class excludes them
1+ # Copyright The IETF Trust 2021-2025, All Rights Reserved
92
103import jsonfield
114from django .db import models
125
13- from ietf .utils .fields import IETFJSONField as FormIETFJSONField , EmptyAwareJSONField as FormEmptyAwareJSONField
6+ from ietf .utils .fields import (
7+ IETFJSONField as FormIETFJSONField ,
8+ EmptyAwareJSONField as FormEmptyAwareJSONField ,
9+ )
1410
1511
1612class EmptyAwareJSONField (models .JSONField ):
17- form_class = FormEmptyAwareJSONField
13+ """JSONField that allows empty JSON values when model specifies empty=False
14+
15+ Taken from/inspired by
16+ https://stackoverflow.com/questions/55147169/django-admin-jsonfield-default-empty-dict-wont-save-in-admin
17+
18+ JSONField should recognize {}, (), and [] as valid, non-empty JSON values.
1819
19- def __init__ (self , * args , empty_values = FormEmptyAwareJSONField .empty_values , accepted_empty_values = None , ** kwargs ):
20+ If customizing the formfield, the field must accept the `empty_values` argument.
21+ """
22+
23+ def __init__ (
24+ self ,
25+ * args ,
26+ empty_values = FormEmptyAwareJSONField .empty_values ,
27+ accepted_empty_values = None ,
28+ ** kwargs ,
29+ ):
2030 if accepted_empty_values is None :
2131 accepted_empty_values = []
22- self .empty_values = [x
23- for x in empty_values
24- if x not in accepted_empty_values ]
32+ self .empty_values = [x for x in empty_values if x not in accepted_empty_values ]
2533 super ().__init__ (* args , ** kwargs )
2634
2735 def formfield (self , ** kwargs ):
28- if 'form_class' not in kwargs or issubclass (kwargs ['form_class' ], FormEmptyAwareJSONField ):
29- kwargs .setdefault ('empty_values' , self .empty_values )
30- return super ().formfield (** {** kwargs })
36+ defaults = {
37+ "form_class" : FormEmptyAwareJSONField ,
38+ "empty_values" : self .empty_values ,
39+ }
40+ defaults .update (kwargs )
41+ return super ().formfield (** defaults )
3142
3243
3344class IETFJSONField (jsonfield .JSONField ): # pragma: no cover
@@ -36,15 +47,21 @@ class IETFJSONField(jsonfield.JSONField): # pragma: no cover
3647 # Remove this class when migrations are squashed and it is no longer referenced
3748 form_class = FormIETFJSONField
3849
39- def __init__ (self , * args , empty_values = FormIETFJSONField .empty_values , accepted_empty_values = None , ** kwargs ):
50+ def __init__ (
51+ self ,
52+ * args ,
53+ empty_values = FormIETFJSONField .empty_values ,
54+ accepted_empty_values = None ,
55+ ** kwargs ,
56+ ):
4057 if accepted_empty_values is None :
4158 accepted_empty_values = []
42- self .empty_values = [x
43- for x in empty_values
44- if x not in accepted_empty_values ]
59+ self .empty_values = [x for x in empty_values if x not in accepted_empty_values ]
4560 super ().__init__ (* args , ** kwargs )
4661
4762 def formfield (self , ** kwargs ):
48- if 'form_class' not in kwargs or issubclass (kwargs ['form_class' ], FormIETFJSONField ):
49- kwargs .setdefault ('empty_values' , self .empty_values )
63+ if "form_class" not in kwargs or issubclass (
64+ kwargs ["form_class" ], FormIETFJSONField
65+ ):
66+ kwargs .setdefault ("empty_values" , self .empty_values )
5067 return super ().formfield (** {** kwargs })
0 commit comments