rgov
rgov

Reputation: 4329

Add descriptive text to the right of field in Django admin

I have a DecimalField in my model that I'd like to show up in the admin interface with a unit to the right of the field, like so:

Desired UI

I think if I add help_text to the field, that will show up below the field. Is there a way to specify it to show up to the right?

Upvotes: 2

Views: 1141

Answers (1)

binpy
binpy

Reputation: 4194

You can handle it with help_text from the forms, overwrite your field in the form, such as the 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)

Upvotes: 3

Related Questions