我查看了堆栈,但找不到任何答案。 所以我有一个这样的模型:
class DynamicModel(models.Model):
config = models.JSONField()
def __str__(self):
return self.config
我现在想要做的是将 json 字段键值对显示为独立字段,并且还能够以相同的方式编辑它们,就像它们每个都是不同的字段一样。这是我能想到的创造
dynamic fields
的最佳方式。所以如果我有一个像这样的 jsonfield:
{
"age": 23,
"name": "John Doe",
"gender": "Male"
}
I would have an `Age` field where I can edit the value or even remove the field alotgether. The changes would then reflect to the `JSONField`.
我的做法是这样的:
# forms.py
from django import forms
from .models import DynamicModel
class DynamicModelForm(forms.ModelForm):
class Meta:
model = DynamicModel
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check if 'config' is in the fields
if 'config' in self.fields:
# Extract JSON data from the instance or initialize an empty dictionary
json_data = self.instance.config if self.instance and 'config' in self.instance else {}
# Loop through the JSON data and add individual fields
for key, value in json_data.items():
# Add a CharField for each key
self.fields[key] = forms.CharField(initial=value, required=False)
self.fields[key].label = key
self.fields[key].widget.attrs.update({'style': 'display:block;'})
# Remove the original JSON field
self.fields.pop('config', None)
def clean(self):
cleaned_data = super().clean()
json_data = cleaned_data.get('config', {})
# Loop through the JSON data and add individual fields
for key, value in json_data.items():
cleaned_data[key] = value
return cleaned_data
# admin.py
from django.contrib import admin
from .models import DynamicModel
class DynamicModelAdmin(admin.ModelAdmin):
form = DynamicModelForm
list_display = ['config']
admin.site.register(DynamicModel, DynamicModelAdmin)
但这似乎并不能解决问题。任何帮助表示赞赏。
一个想法可能是使用一个空的
ModelForm
,然后动态填充:
class DynamicModelForm(forms.ModelForm):
class Meta:
model = DynamicModel
fields = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields.pop('config', None)
json_data = self.instance.config or {}
for key, value in json_data.items():
# Add a CharField for each key
self.fields[key] = forms.CharField(initial=value, required=False)
self.fields[key].label = key
self.fields[key].widget.attrs.update({'style': 'display:block;'})
def save(self, *args, **kwargs):
json_data = self.instance.config or {}
cleaned_data = self.cleaned_data
data = self.cleaned_data
for key, value in json_data.items():
config[key] = cleaned_data.get(key, value)
self.instance.config = config
return super().save(*args, **kwargs)
因此,我们添加了字段,或多或少就像您自己定义的那样。当我们想要保存项目时,我们将字段重新分组到字典中,并将其保存到实例中。