使用模板中的表单访问模型字段的属性

时间:2014-06-16 20:47:09

标签: python django

我们正在构建一个Django应用程序并拥有一个包含两个类的模型:

class Category_Specialization (models.Model):
  name = models.CharField(max_length=50)

class Specialization (models.Model):
  specialization_name = models.CharField (max_length=60)
  category = models.ForeignKey(Category_Specialization)

class User (models.Model):
[...]
  specialization = models.ManyToManyField (Specialization)

我们创建了一个表单,用于显示字段用户的复选框:

class SpecializationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
  super(UserServiceForm, self).__init__(*args, **kwargs)
  self.helper = FormHelper()
  self.helper.form_class = 'form-horizontal'
  self.helper.render_hidden_fields = True
  self.helper.form_tag = False
  self.helper.label_class = 'col-lg-2'
  self.helper.field_class = 'col-lg-10'

  self.helper.layout = Layout(
    'specialization',
    FormActions(
      Submit('submit', 'Create profile'),
    )
  )
  class Meta:
    model = User
    fields = ['specialization']
    widgets = {
    'specialization' : forms.CheckboxSelectMultiple,
    }

我们已经定义了4个类别,2个类别专业化。现在我们想要在模板中显示所有类别,并在其中进行专业化。

所以,在我们的观点中:

list_categories = Category_Specialization.objects.order_by('name')
forms = UserSpecializationForm(request.POST or None, instance=request.user)

return render (request, 'users/profile/specialization.html', {
  'list_categories' : list_categories,
  'forms' : forms,
})

在我们的模板中,我们尝试:

<form action="{% url 'users:specialization'  %}" method="post">
{% csrf_token %}

{% for category in list_categories %}
  <fieldset>
  <legend>{{category.name}}</legend>
  {% for form in forms %}
    {% for form_field in form %}
      {% if form_field.field.category.name == category.name %}
       {% load crispy_forms_tags %}
       {{form_field}}
       {% crispy form %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  </fieldset>
{% endfor %}

问题是我们的IF无法正常工作,因为我们无法正确访问类别名称

那么,我们如何访问for-loop中字段的类别名称以在正确的类别中显示表单?

谢谢。

2 个答案:

答案 0 :(得分:1)

对我而言,使用自定义窗口小部件定义是更好的方法。

放入你的form.py:

class SpecializationByCategory(forms.CheckboxSelectMultiple):
def render(self, name, value, attrs=None, choices=()):
    if value is None: value = []
    has_id = attrs and 'id' in attrs
    final_attrs = self.build_attrs(attrs, name=name)
    output = [u'<ul>']
    # Normalize to strings
    str_values = set([force_unicode(v) for v in value])
    categories = Category_Specialization.objects.all()
    #for supercategory in supercategories:
    for category in categories:
        output.append(u'<li>%s</li>'%(category.name))
        output.append(u'<ul>')
        del self.choices
        self.choices = []
        specializations = Specialization.objects.filter(category=category)
        print specializations
        for specialization in specializations:
            self.choices.append((specialization.id,specialization.specialization_name))
            for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
                if has_id:
                    final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                    label_for = u' for="%s"' % final_attrs['id']
                else:
                    label_for = ''
                cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
                option_value = force_unicode(option_value)
                rendered_cb = cb.render(name, option_value)
                option_label = conditional_escape(force_unicode(option_label))
                output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
        output.append(u'</ul>')
        output.append(u'</li>')
    output.append(u'</ul>')
    return mark_safe(u'\n'.join(output))

在SpecializationForm中,使用小部件:

widgets = {
        'specialization' : SpecializationByCategory,
    }

希望它有所帮助。

答案 1 :(得分:0)

在您看来,我建议使用字典来传递它。这种逻辑在服务器端可以更好地工作。然后,您可以循环模板中的键和值。像这样:

list_categories = {'cat1': {'spec1': foo1, 'spec2': bar1, }, 'cat2': {'spec1': foo2, 'spec2': bar2, }, }

然后在模板上:

{% load crispy_forms_tags %}
{% for category, specs in list_categories %}
    <fieldset>
        <legend>{{category.name}}</legend>
        {% for spec, name in specs %}
            {{ form_field }}
        {% endfor %}
    </fieldset>
{% endfor %}

负载应该在任何循环之外,因此您不必每次都加载尝试并加载它。它可以放在模板的顶部。