2

我有 3 个模型,每个模型都有不同的领域。对于其中 2 个模型,我可以使用通用表单(通过 Django 的 create_object)来请求数据。我编写了一个接受模型名称并将用户发送到通用表单的函数

url(r'^add_(?P<modelname>\w+)/$', generic_add),

def generic_add(request, modelname):
    mdlnm_model = models.get_model('catalog',modelname)
    return create_object(request,
        model = mdlnm_model,
        template_name = 'create.html',
        post_save_redirect = '/library/',
        extra_context = {'func': 'Create ' + modelname},
        login_required =  'True'
    )

对于第 3 个模型,我定义了一个 ModelForm 类,以便在用户看到表单时可以省略该模型中的一个字段。

url(r'^create_actor/$', create_object, Actor_Input, name='db_actor_create'),

Actor_Input = {
   'form_class': ActorForm,
   'template_name': 'create.html',
   'post_save_redirect': '/library/',
   'extra_context': {'func': 'Create Actor'},
   'login_required': 'True'
}

class ActorForm(forms.ModelForm):
    class Meta:
          model = Actor
          fields = ('name','age','height','short_description',
                   'long_description')

如果定义的 ModelForm 存在,Django 是否有办法显示它,但如果尚未制作定义的表单,则显示完全通用的表单?我预计会创建更多模型,并且不想像 Actor 那样为需要拆分的每个模型创建一个 url。

所以换一种说法,我想改变 generic_add 函数,让它使用 ActorForm(如果存在的话),否则使用通用 ModelForm。我知道如何检查 ActorForm 类的存在,但是如果我希望它也是动态的呢?类似于检查是否存在:modelname + 'Form'。我不确定如何动态地将用户发送到预定义的表单(如果存在)。

有什么建议么?有没有更好的方法来看待这个问题?

4

2 回答 2

1

以下是我可能会如何处理您正在尝试做的事情:

url(r'^add_(?P<model_name>\w+)/$', generic_add),

model_presets = {
    'Actor': {
        'extra_context': {'func': 'Create Actor'},
        'form_class': ActorForm,
        'login_required': True,
        'post_save_redirect': '/library/',
        'template_name': 'create.html'
    },
    'default': {
        'extra_context': {'func': 'Oops, something went wrong if you are seeing \
                                   this, it should have been overwritten!'},
        'form_class': None,
        'login_required': True,
        'model': None,
        'post_save_redirect': '/library/',
        'template_name': 'create.html'
    }
}

def _create_object_conf_helper(request, model_name):
    if model_name in model_presets:
        conf = model_presets[model_name]
    else:
        try:
            named_model = models.get_model('catalog', model_name)
        except:
            # do something here to protect your app!
        conf = model_presets['default']
        conf['model'] = named_model
        conf['extra_context']['func'] = 'Create %s' % model_name
    conf['request'] = request
    return conf

def generic_add(request, model_name):
    return create_object(**_create_object_conf_helper(request, model_name))

我没有对此进行测试,但它应该可以正常工作,如果不是,请告诉我,因为我可能想在我自己的项目中使用类似的东西。

此外,您还可以更进一步,为 model_presets dict 创建另一个层,以允许类似的辅助函数为您可能正在使用的任何其他通用视图创建配置。

顺便说一句,没有必要将 True 括在引号中,只需记住大写 T 而不是 rue,它将解析为 1 位布尔常量。使用 'True' 使用(粗略的)最少 32 位作为字符串。两者都将在 if 语句中测试为真,因此这没什么大不了的。另一方面,使用 'False' 将无法按预期工作,因为这又是一个字符串而不是布尔值,因此也将测试为真。

请参阅http://docs.python.org/library/stdtypes.html#truth-value-testing

于 2010-04-26T08:25:06.380 回答
0

It would be very helpful to see this function you are talking about.
The normal way of using create_object is indeed to specify the model or form that you want to use which would result in three URLs in your case.

From the documentation:

Required arguments:

  • Either form_class or model is required.
    If you provide form_class, it should be a django.forms.ModelForm subclass. Use this argument when you need to customize the model's form. See the ModelForm docs for more information.
    Otherwise, model should be a Django model class and the form used will be a standard ModelForm for model.

You see, you can specify the form to use. Maybe this already helps you, but without further information we cannot do more.

于 2010-03-04T07:29:10.130 回答