在Django管理员的change_form中的两个模型字段之间添加自定义html [英] Add custom html between two model fields in Django admin's change_form
问题描述
class Book(models.Model):
name = models .CharField(max_length = 50)
library = models.ForeignKeyField('Library')
类库(models.Model):
name = models.CharField(max_length = 50 )
address = models.CharField(max_length = 50)
tel = models.CharField(max_length = 50)
有没有一种很好的方式在Library change_form模板中的名称和地址之间添加一些html(一个只读输入字段)。我正在覆盖管理员/includes/fieldset.html ,但它变得凌乱,我找不到一种方式来显示我想要的HTML。例如,如果我想添加html,显示图书馆在名称字段下方的图书数量,我会这样做:
{%for line in line%}
...
{%if field.field.name =='name'%}
{{field.field}}
< div class =form-row total_books>
< div>
< label for =total_books>书籍总数:< / label>
< input type =textmaxlength =10name =totbooksid =totbooksreadonly =readonly>
< / div>
< / div>
{%else%}
{{field.field}}
{%endif%}
...
{%endfor%}
新解决方案:
我发现我觉得更好一些,但是我不知道为什么我得到这个错误:PresupuestoAdmin.readonly_fields 1 ,'name'不是PresupuestoAdmin的可调用或属性,也可以在模型Presupuesto中找到。似乎名称字段未添加到管理员使用的表单中。
类FoooAdminForm(forms.ModelForm):
name = models.CharField(max_length = 100)
类Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
fieldsets =(
(无,{'fields ':('id','name','date')}),
)
readonly_fields =('id','name')
admin.site。注册(Foo,FooAdmin)
class FooAdminForm(forms.ModelForm):
name = forms.CharField(max_length = 100,
widget = forms.TextInput(attrs = {'readonly':'readonly'}))
def __init __(self,* args,** kwargs):
super(FooAdminForm,self).__ init __(* args,** kwargs)
self.fields ['name '] .required = False
如果kwargs.has_key('instance'):
instance = kwargs ['instance']
#做某事如果你想要
class Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
fieldsets =(
(无,{
'fields':('id','name',),})
)
readonly_fields = ('id',)
Let's say I've two models:
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)
Is there a nice way to add some html(a readonly input field) between name and address in the Library change_form template?. I'm doing it overriding admin/includes/fieldset.html but it's getting messy and I can't find a way to display the html exactly where I want to. For example, if I want to add html displaying the amount of books that the library has below the name field I woul do this:
{% 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 %}
New solution:
I've found a nicer way I think, but I don't know why am I getting this error: "PresupuestoAdmin.readonly_fields1, 'name' is not a callable or an attribute of PresupuestoAdmin' or found in the model 'Presupuesto' ". It seems that the 'name' field is not added to the form used by the admin.
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)
class FooAdminForm(forms.ModelForm):
name = forms.CharField(max_length=100,
widget=forms.TextInput(attrs={'readonly': 'readonly'}))
def __init__(self, *args, **kwargs):
super(FooAdminForm, self).__init__(*args, **kwargs)
self.fields['name'].required = False
if kwargs.has_key('instance'):
instance = kwargs['instance']
# do something with the instance here if you want
class Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
fieldsets = (
(None, {
'fields': ('id', 'name',),})
)
readonly_fields = ('id',)
这篇关于在Django管理员的change_form中的两个模型字段之间添加自定义html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!