get_model 相当于 ModelForms 吗?

发布于 2024-09-10 10:06:26 字数 470 浏览 4 评论 0原文

我有多个 ModelForm 类,每个类代表一个不同的模型。我想要一个通用的“创建”函数,它根据 URL 参数加载指定的模型表单。可以用这个动态加载模型:

model_name = 'TestModel'
m = get_model('AppLabel', model_name)

有谁知道如何为 ModelForms 实现相同的效果,例如:

modelform_name = 'TestModelForm'
f = get_form('AppLabel', modelform_name)
if f.is_valid():
    ...

我想不出一种方法来使用通用视图来做到这一点 - 它们需要传递 ModelForm,而不是只是它的名字。如果我使用 get_model 获取模型,然后将其传递给通用视图,它将显示一个表单,但我无法排除模型字段。

TIA 提供任何提示

I have a multiple ModelForm classes that each represent a different Model. I would like to have a generic 'create' function that loads the specified model form based on a URL parameter. It is possible to load a model dynamically with this:

model_name = 'TestModel'
m = get_model('AppLabel', model_name)

Does anyone know how I can achieve the same for ModelForms, something like:

modelform_name = 'TestModelForm'
f = get_form('AppLabel', modelform_name)
if f.is_valid():
    ...

I can not think of a way to do this with generic views - they require the ModelForm to be passed, rather than just its name. If I get the model with get_model then pass that to the generic view it will display a form but I am unable to exclude model fields.

TIA for any tips

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2024-09-17 10:06:26

当您创建 ModelForm 时,它不会将自身注册到其模型的应用程序中。 (基于经验和快速浏览源代码)。

以下是我能想到的一些其他选项:

  1. 所有 ModelForm 类都存在于单个模块中:基于该模块在该模块上使用 getattr细绳。

  2. ModelForm 分布在许多模型中,并且您拥有合理的 (<30) 表单数量:
    创建从您期望的表单字符串到 ModelForm 类的字典映射。例如:

    from some_app.forms import FirstModelForm
    从 another_app.forms 导入 SecondModelForm
    from extra_app.forms import FirstModelForm as DifferentialAppFirstModelForm # 将允许轻松管理冲突的名称。
    
    表单映射 = {
        'FirstModelForm': FirstModelForm,
        'SecondModelForm':SecondForm,
        'AdditionalAppFirstModelForm':AdditionalAppFirstModelForm,
    }
    
    request_form_class = request.POST.get('form_class')
    f = form_mapping.get(request_form_class)(request.POST)
    
    如果 f.is_valid():
        f.保存()
    
  3. 您正在处理大量表单:为您的 ModelForm 创建基类,或在运行时替换BaseModelFormMetaclass。您必须“自动”处理名称冲突、同一 Model 的重复 ModelForm 等问题,因此请做好应对一些头痛的准备。如果你能成功的话,那就太棒了。

就我个人而言(正如您可能看到的),我只会选择选项#2。

When you create a ModelForm it does not register itself with its model's app. (Based on experience and a quick browse through the source).

Here are some otheroptions I can think of:

  1. All ModelForm classes exist in a single module: Use getattr on that module based on the string.

  2. ModelForm's are spread out among many models and you have a reasonable (<30) amount of forms:
    Create a dictionary mapping from form strings you expect to ModelForm classes. For example:

    from some_app.forms import FirstModelForm
    from another_app.forms import SecondModelForm
    from additional_app.forms import FirstModelForm as AdditionalAppFirstModelForm # Will allow for managing conflicting names easily.
    
    form_mapping = {
        'FirstModelForm': FirstModelForm,
        'SecondModelForm': SecondForm,
        'AdditionalAppFirstModelForm': AdditionalAppFirstModelForm,
    }
    
    request_form_class = request.POST.get('form_class')
    f = form_mapping.get(request_form_class)(request.POST)
    
    if f.is_valid():
        f.save()
    
  3. You're dealing with a lot of forms: Create a baseclass for your ModelForm, or replace the BaseModelFormMetaclass at runtime. You'll have to deal with issues such as name conflicts, duplicate ModelForms for the same Model "automagically", so prepare for some headaches. It would be pretty rad if you could pull it off.

Personally (as you can probably see), I'd just go with option #2.

空城仅有旧梦在 2024-09-17 10:06:26

另一种方法是用名为 forms 的包替换 forms.py。然后,在该包内的 __init__.py 中,导入所有 ModelForm

然后你可以使用 sdolan 的选项#1。

An alternate method for this is to replace forms.py with a package called forms. Then, in __init__.py within that package, import all your ModelForms.

Then you can use sdolan's Option #1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文