0

我有两个模型之间的多对多关系:

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 %}
4

0 回答 0