制作Django ModelForm模型 - 通用?

时间:2011-08-15 17:53:52

标签: django forms

我正在面对Django的一个有趣的情况,我希望有人会看到解决方案,或者至少可以给我一个提示。

我正在尝试制作ModelForm模型 - 通用。我不知道这是不是应该做的事情,但现在就这样了。

这很好用:

元组引用模型

# settings.py
SPECIES = (
    ('TIG', 'Tiger'),
    ('SHR', 'Shark'),
)

用于创建动态对象的网址

# urls.py
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('species.views',
    url(r'^add/$', 'add_animal', name='add_animal'),
)

动物模型及其中的两个孩子

# models.py
from django.db import models
from django.conf import settings

class Animal(models.Model):
    name = models.CharField(max_length=100)
    nickname = models.CharField(max_length=100)
    species = models.CharField(max_length=3, choices=settings.SPECIES)

class Tiger(Animal):
    fangs_size = models.IntegerField()

class Shark(Animal):
    color = models.CharField(max_length=20)

显示表单的视图

通过GET参数选择正确的模型。

# views.py
def add_animal(request):
    if request.method == "GET":
        if request.GET['model_name']:
            model_name = request.GET['model_name']
    else:
        model_name = 'Animal'

    print "Model name is: %s" % model_name

    model = get_model("species", model_name)
    form = AnimalForm(model=model)

    return create_object(
        request,
        model=model,
        post_save_redirect=reverse('index'),
        template_name='species/my_form.html',
    )

模板

# my_form.html
<!doctype html>
<html>
    <head>
        <title>Adding an animal</title>
    </head>
    <body>
        <h1>Add an animal to the farm</h1>
        <form>
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

当我访问 / add?model_name = tiger 时,我会显示正确的表单。

现在,假设我要隐藏昵称字段。然后我需要使用自定义的ModelForm。如何用正确的模型实现它?那是我的问题。

以下是表格?

# forms.py
from species.models import Animal
from django import forms

class AnimalForm(forms.ModelForm):

    class Meta:
        model = Animal

    def __init__(self, *args, **kwargs):
        model = kwargs.pop('model')
        super(AnimalForm, self).__init__(*args, **kwargs)
        self.Meta.model = model

视图变为:

# views.py
...
model = get_model("species", model_name)
form = AnimalForm(model=model)

return create_object(
    request,
    # model=model,            # Need for customization
    # form_class=AnimalForm,  # With the class name, how to pass the argument?
    form_class=form,          # Not sure this can be done, I get this error: 'AnimalForm' object is not callable
    post_save_redirect=reverse('index'),
    template_name='species/my_form.html',
)
...

我的目标是能够创建稍后继承自Animal的新模型,将它们添加到SPECIES元组并完成。这可以实现吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

您可能希望使用django.forms.models.modelform_factory - 它的模型类和参数中的exclude元组。您可以在视图中使用它来动态创建表单类。

form_class = modelform_factory(model, exclude=('nickname',))
相关问题