比方说,我有两个型号:在Django管理的change_form两个型字段之间添加自定义HTML
class Book(models.Model):
name = models.CharField(max_length=50)
library = models.ForeignKeyField('Library')
class Library(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
tel = models.CharField(max_length=50)
有一个很好的方式,在名称和地址之间添加一些HTML(只读输入字段)图书馆change_form模板?我正在重写admin/includes/fieldset.html,但它变得混乱,我无法找到一种方法来显示HTML完全在我想要的地方。例如,如果我想添加HTML显示,该库具有以下名称字段竟被我做这个的书量:
{% for field in line %}
...
{% if field.field.name == 'name' %}
{{ field.field }}
<div class="form-row total_books">
<div>
<label for="total_books">Total books:</label>
<input type="text" maxlength="10" name="totbooks" id="totbooks" readonly="readonly">
</div>
</div>
{% else %}
{{ field.field }}
{% endif %}
...
{% endfor %}
新的解决方案:
我已经找到了一种更好的方式我想,但我不知道为什么我会收到此错误:“PresupuestoAdmin.readonly_fields 1,'name'不是可调用的或PresupuestoAdmin的属性'或在模型'Presupuesto'中找到的”。看起来'name'字段没有添加到管理员使用的表单中。
class FoooAdminForm(forms.ModelForm):
name = models.CharField(max_length=100)
class Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
fieldsets = (
(None, { 'fields': ('id', 'name', 'date')}),
)
readonly_fields = ('id', 'name')
admin.site.register(Foo, FooAdmin)
谢谢乌迪,我想我张贴的解决方案可能是更清洁。 – mfalcon 2011-06-08 13:52:03
我添加了一个更好的解决方案(http://stackoverflow.com/questions/6239966/add-custom-html-between-two-model-fields-in-django-admins-change-form/6343271#6343271).. (ahm ...我现在应该投我以前的答案吗?) – Udi 2011-06-14 12:14:08