0

In my Django app, I have a page.html that includes forms from multiple models like so:

# models.py
class OneModel(models.Model):
    foo = models.CharField(...)
    bar = models.CharField(...)

# forms.py
class OneForm(forms.ModelForm):
    class Meta:
        model = OneModel

On save, in addition to the POST data, I add a prefix to all the forms, for example:

form1 = OneForm(prefix=OneForm.Meta.model._meta.module_name)

Then, I send the POST request to be saved and send the next data to the __init__ form:

# views.py
form_data = {'foo': 'fo-fo-fo',    # from request.POST
             'bar': 'bar-bar-bar'} # -//-//-
form1 = OneForm(form_data)         # without prefix

Afterwards, when the template is being put together, I get input from the form with my data (fo-fo-fo, bar-bar-bar), but without the prefix in input names, which is not what I want.

But if I add the prefix in the __init__ part instead I get back the form without my data:

# views.py
form1 = OpenForm(prefix=OneForm.Meta.model._meta.module_name, form_data)

How can I get the results of the form with both the data I need and the prefix I added?

4

1 回答 1

1

您不需要form_datarequest.POST. 您可以将表单实例创建为form1 = OneForm(request.POST, prefix=OneForm.Meta.model._meta.module_name).

于 2012-09-13T17:03:33.953 回答