0

我试图在我的模板中显示一个 ManyToManyField:

class GvtCompo(models.Model):
    startDate=models.DateField(max_length=10, blank=False, null=False)
    endDate=models.DateField(max_length=10, blank=False, null=False)
    gvtCompo= models.CharField(max_length=1000, blank=False, null=False)

class Act(models.Model):
    gvtCompo= models.ManyToManyField(GvtCompo)

在我看来,我可以显示对象,没问题:

for gvtCompo in act.gvtCompo.all():
    print gvtCompo.gvtCompo

在我的模板中,我有一个带有此代码的“GvtCompo 对象”列表(不好):

{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field }}
    </div>
{% endfor %}

我试图让它变得更好,但下面的代码不起作用(没有出现):

{% for field in form %}
    {% if field.name == "gvtCompo" %}
        {% for gvtCompo in field.gvtCompo.all %}
            {{ gvtCompo.gvtCompo }}
        {% endfor %}
    {% endif %}
{% endfor %}

怎么了?

*编辑:*

如果我不使用表单而是传递给 render_to_response 的模型(行为)实例,它会显示 ManyToManyField 值

{% for gvtCompo in field.gvtCompo.all %}

变成

{% for gvtCompo in act.gvtCompo.all %}

但是没有表单域了,所以不能修改、验证和保存!

4

2 回答 2

1

您正在跳过一个步骤。您首先需要创建一个表单。

在 forms.py 中,您可以创建一个 ModelForm。ModelForm 是基于您的模型的表单:

from django.forms import ModelForm from myapp.models import Act

class ActForm(ModelForm):
   class Meta:
       model = Act

然后你的观点:

from myapp.models import Act
from myapp.forms import ActForm


def add_view(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = ActForm() # Creating a empty form.
    return render_to_response("template.html", {
        "form": form,
    })

def edit_view(request):
    obj = Act.objects.get(pk=1)

    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = ActForm(instance=obj) # Creating form pre-filled with obj.
    return render_to_response("template.html", {
        "form": form,
    })

如果您想多次实施这种情况。干燥:https ://docs.djangoproject.com/en/1.5/topics/class-based-views/intro/#handling-forms-with-class-based-views

在您的 template.html 中:

{{ form }}

免责声明:此代码未经测试。 https://docs.djangoproject.com/en/1.5/ref/forms/ https://docs.djangoproject.com/en/1.5/topics/forms/modelforms/

更新:

您可以将多个表单传递给<form>...</form>模板中的一个。所以创建两个表格。一份法案表格(见上文)和一份 GvtCompo 表格集。formset 包含所有与 Act 相关的 GvtCompo。

from django.forms.models import modelformset_factory

act = Act.objects.get(pk=1) #The same act as you used to create the form above.

GvtFormSet = modelformset_factory(GvtCompo)
formset = GvtFormSet(queryset=act.gvtCompo.all())

模板可以是:

<form ...>
{% for field in form %}
    {% if field.name == "gvtCompo" %}
        {{ formset }}
    {% else %}
        {{ field }}
    {% endif %}
{% endfor %}
</form>

注意:如果您的表单和表单集有冲突的字段名称,请使用 prefix="some_prefix": Django - Working with multiple forms

于 2013-07-28T11:45:08.387 回答
0

循环表单时,它应该是:

{% for field in form %}
    {% if field.name == "gvtCompo" %}
        {% for gvtCompo in form.instance.gvtCompo.all %}
            {{ gvtCompo.gvtCompo }}
        {% endfor %}
    {% endif %}
{% endfor %}

字段本身没有相关field.gvtCompo

于 2013-07-28T10:41:13.653 回答