4

我已经搜索了几天,试图找到一种 DRY 方法来使用 django 的内置验证和表单呈现从 JsonField 创建动态表单。我还没有找到一个包含 django 验证和渲染的解决方案,所以我想为此创建一个模块,但我很难弄清楚这些类是如何工作的。

我尝试了几种不同的方法,例如:

模型.py

from django.contrib.postgres.fields import JSONField
from django.db import models

class Forklift(models.Model):
    name = models.CharField(max_length=50)
    json_fields = JSONField()

表格.py

from django import forms
from .models import Forklift

fields_dict = {
    'name': forms.CharField(max_length=25),
    'number': forms.IntegerField()
}

class ModelForm(forms.ModelForm):
    class Meta:
        model = Forklift
        exclude = ['json_fields']

class DynamicForm(forms.Form):
    pass

这是我得到的错误:

错误

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

它看起来像forms.Form子类forms.BaseFormforms.DeclaritaveFieldsMetaclass但我不知道如何子类 forms.Form 以从字典中传递动态字段。我还尝试了以下方法:

视图.py

from django import forms
from django.shortcuts import render
from .forms import fields_dict

def dynamic_form(request): #__init_subclass__() takes no keyword arguments
    class NewForm(forms.BaseForm, fields=fields_dict): # also tried with fields=fields_dict
        pass
    form = NewForm(request.POST or None)
    return render(request, 'template.html', {'form': form})

def dynamic_form(request): # form will only render once then disappear
    content = {}
    context = {}

    dynamic_form = type('dynamic_form', (DynamicForm,), fields_dict)
    form = dynamic_form(content)

    context = {
        'form': form,
    }
    return render(request, 'activity/dynamic_form.html', context)


def dynamic_form(request): # module 'django.forms' has no attribute 'DeclaritaveFieldsMetaclass'
    class NewForm(forms.BaseForm, metaclass=DeclarativeFieldsMetaclass(MediaDefiningClass),  data=fields_dict):
        pass
    form = NewForm(request.POST or None)
    return render(request, 'template.html', {'form': form})

我不只是在问一个答案,我真正想知道的是有人如何遍历所有这些类来弄清楚如何对它们进行子类化。我想我可以通过自己编写所有逻辑、验证等来弄清楚如何完成这项工作,但我希望这是一个其他人可以使用的模块。

网址.py

from django.urls import path

from .views import dynamic_form, test

urlpatterns = [
    path('form/', dynamic_form),
]

编辑

我最初是从记忆中输入代码并且有一些拼写错误。我从我的代码库中复制了代码并包含了 urls.py。

4

1 回答 1

1

看看这个第三方包django-entangled,然后将您的表单重写为

from entangled.forms import EntangledModelForm

class DynamicForm(EntangledModelForm):
    name = forms.CharField(max_length=25)
    number = forms.IntegerField()

    class Meta:
        model = Forklift
        entangled_fields = {'json_fields': ['name', 'number']}

然后在使用标准 Django 表单时像往常一样渲染表单。

于 2021-04-13T21:47:32.857 回答