如何根据从 Django 中的表单请求传递的 ID 使用 JSONSchemaField 创建动态表单?
How to create dynamic form using JSONSchemaField based on ID passed from the form request in Django?
如果我在 forms.py 中将 ID 作为静态值,则表单可以正确呈现,但当我使用从表单调用中获得的 ID 时,表单将无法正确呈现
views.py
def assetAddJsonView(request,pk):
form = AssetAddjsonForm(id = pk)
context = {
'form': form
}
return render(request, 'asset_add_json.html', context)
forms.py
from django_jsonforms.forms import JSONSchemaField
class AssetAddjsonForm(Form):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('id')
super(AssetAddjsonForm, self).__init__(*args, **kwargs)
type_json_schema = Types.objects.values_list('details').get(id=1) # details contains schema object
type_json_schema = list(type_json_schema)[0]
add_asset = JSONSchemaField(schema = type_json_schema, options = options)
而不是传递 id=1 我想传递我在 self.request
中得到的值
我提到了这个 link
提前致谢
我找到了问题的答案
这是我在 formclass
中更改的代码
def assetAddJsonView(request,pk):
def __init__(self, *args, **kwargs):
ids = kwargs.pop('id')
super(AssetAddjsonForm, self).__init__(*args, **kwargs)
type_json_schema = Types.objects.values_list('details').get(id=ids)
self.fields['add_asset'] = JSONSchemaField(schema=list(type_json_schema)[0], options = options)
如果我在 forms.py 中将 ID 作为静态值,则表单可以正确呈现,但当我使用从表单调用中获得的 ID 时,表单将无法正确呈现
views.py
def assetAddJsonView(request,pk):
form = AssetAddjsonForm(id = pk)
context = {
'form': form
}
return render(request, 'asset_add_json.html', context)
forms.py
from django_jsonforms.forms import JSONSchemaField
class AssetAddjsonForm(Form):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('id')
super(AssetAddjsonForm, self).__init__(*args, **kwargs)
type_json_schema = Types.objects.values_list('details').get(id=1) # details contains schema object
type_json_schema = list(type_json_schema)[0]
add_asset = JSONSchemaField(schema = type_json_schema, options = options)
而不是传递 id=1 我想传递我在 self.request
中得到的值我提到了这个 link
提前致谢
我找到了问题的答案 这是我在 formclass
中更改的代码def assetAddJsonView(request,pk):
def __init__(self, *args, **kwargs):
ids = kwargs.pop('id')
super(AssetAddjsonForm, self).__init__(*args, **kwargs)
type_json_schema = Types.objects.values_list('details').get(id=ids)
self.fields['add_asset'] = JSONSchemaField(schema=list(type_json_schema)[0], options = options)