在Django管理员的字段右侧添加描述性文本

时间:2017-01-02 18:25:14

标签: django django-admin

我的模型中有一个DecimalField,我想在管理界面中显示一个位于该字段右侧的单位,如下所示:

Desired UI

我认为如果我在字段中添加com.android.dex.DexException: Too many classes in --main-dex-list, main dex capacity exceeded ,则会显示在字段下方。有没有办法指定它显示在右边?

1 个答案:

答案 0 :(得分:3)

您可以使用help_text中的forms来处理它,覆盖表单中的字段,例如css

from django.db import models
from django.contrib import admin
from django import forms
from your.models import Post


class PostForm(forms.ModelForm):
    ethanol = forms.FloatField(
        label='This is Ethanol',
        max_value=10,
        min_value=0,
        widget=forms.NumberInput(
            attrs={
                'class': 'whatever',
                'style': 'position:relative'
            }
        ),
        help_text='<span style="position:absolute;right:0">g/mL</span>'
    )

    class Meta:
        model = Post
        fields = '__all__'


class PostAdmin(admin.ModelAdmin):
    form = PostForm
    list_display = ['ethanol', 'id']


admin.site.register(Post, PostAdmin)