是否可以將某些屬性寫入模型字段,稍後可以使用該字段來區分模板中的不同字段?如何將自定義屬性設置爲可以在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