是否可以将某些属性写入模型字段,稍后可以使用该字段来区分模板中的不同字段?如何将自定义属性设置为可以在Django中使用ModelForm的模板中使用的模型?
model.py
from django.db import models
class Person(models.Model):
first_name = models.CharField("i am the Label", max_length=30)
last_name = models.CharField("i am other Label", max_length=30, customattr="Custom")
forms.py
class PersonForm(ModelForm):
class Meta:
Person
template.html
<form action="" method="post">{% csrf_token %}
{% for field in form %}
{% ifequal field.customattr 'Custom' %} # HOW COULD THIS WORK?
<p>Hello world.</p>
{{ field }}
{% else %}
<p>This is not Custom</p>
{{ field }}
{% endifequal %}
{% endfor %}
<input type="submit" value="Submit" />
</form>
任何提示?
可能重复? http://stackoverflow.com/questions/8407837/django-custom-attributes-for-model-fields – 2013-05-13 18:25:43