如何以逗号分隔列表显示多对多相关对象

时间:2013-06-06 14:20:53

标签: django django-forms many-to-many

我在两个模型之间有很多关系:

class CouncilDistrict(models.Model):
    district = models.IntegerField('council district')
    zoning_case = models.ManyToManyField('ZoningCase')

    def __unicode__(self):
        return str(self.district)

class ZoningCase(models.Model):
    accepted_by = models.ForeignKey(Employee, related_name='employee_case_acceptedby')
    acres = models.DecimalField(max_digits=8, decimal_places=4)
    ...

根据是否正在编辑ZoningCase,我想更改相关议会区的显示方式。如果编辑,我使用SelectMultiple小部件,并且工作正常。但是,当不编辑时,我想在TextInput中将CouncilDistrict对象列表显示为逗号分隔列表。那种作品。

我的问题和问题是,TextInput中的显示显示:

[<CouncilDistrict: 1>, <CouncilDistrict: 3>...

而不是

1, 3

如何仅显示区域字段,而不是模型名称和区域?感谢。

编辑:视图和模板,根据要求,删除了许多无关字段

查看:

def view_case(request, aCaseId):
    zc = ZoningCase.objects.get(pk=aCaseId)
    form = ViewZoningCaseForm({'id':zc.id,
                           'accepted_by':zc.accepted_by,
                           'acres':zc.acres,
                           'council_districts':CouncilDistrict.objects.filter(zoning_case=zc),
                           'zrt_date':format_date(zc.zrt_date)
                           })
    variables = RequestContext(request, {'form':form, 'case_number':zc.case_number, 'case_id':zc.id, 'editing':False})
    return render_to_response('viewZoningCase.html', variables)

形式:

class ViewZoningCaseForm(forms.Form):
    council_districts = forms.CharField(label='Council Districts', required=False,widget=forms.TextInput(attrs={'readonly':'readonly'}))
    zrt_date = forms.DateField(label='ZRT Date', required=False, widget=forms.DateInput(attrs={'readonly':'readonly'}), input_formats=['%m/%d/%Y', '%Y-%m-%d'])
    acres = forms.DecimalField(label='Size of Request', required=False, max_digits=10, decimal_places=4, widget=forms.TextInput(attrs={'readonly':'readonly'}))
    accepted_by = forms.ModelChoiceField(queryset=Employee.objects.none(), required=False, label='Accepted By', widget=forms.Select(attrs={'readonly':'readonly', 'disabled':True}))

def __init__(self, *args, **kwargs):
    super (ViewZoningCaseForm, self).__init__(*args, **kwargs)
    perm = Permission.objects.get(codename='can_be_zoning_planner')
    self.fields['accepted_by'].queryset = Employee.objects.filter(user__user_permissions=perm)

模板(此时非常基本 - 这是获得mgt批准的初步方法):

{% block content %}
<div id="zoningCaseDiv">
    <form method="post" action="." id="zoningCaseForm">
        {% csrf_token %}
        <div id="tab-1">
                <table id="zoningCaseDetailsTable">
                <tr>
                    <td>{{form.case_number.label_tag}}</td>
                    <td>{{form.case_number}}</td>
                    <td>{{form.gis_case_id.label_tag}}</td>
                    <td>{{form.gis_case_id}}</td>
                </tr>
                <tr>
                    <td>{{form.closed.label_tag}}</td>
                    <td>{{ form.closed }}</td>
                    <td>{{form.council_districts.label_tag}}</td>
                    <td>{{form.council_districts}}</td>
                </tr>
            </table>
        </div>
    </form>
</div>
{% endblock %}

0 个答案:

没有答案