I want to right align numeric fields in the admin interface of my Django app. For “normal” editable fields this works ok by overriding formfield_for_dbfield(self, db_field, **kwargs): for the ModelAdmin class:
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(MyModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name in ("total_amount", "total_discount_amount"):
field.widget.attrs['style'] = 'text-align:right;'
return field
When a field is defined as a read-only field, it by passes this method and gets rendered in a different way (apparently just as a css styled paragraph tag).
Is there a way to right align a read-only field? I found this question on SO but its problem is a different one from mine, I think…
Why not just use css? Define a an /admin/base.html template in which you load a custom css that specifies:
If you need to restrict the styiling of certain readonly fields you could still use css by doing something like:
or even specific fields:
Addendum
I see you are using Grappelli, in that case you could do:
If you need to style only certain readonly paragraphs and not all of them you could still prefix the CSS rule with the css class of the parent element, usually the class is named after the field name, so if you have a file called “total” it would be:
I think writing a custom filter for a presentation issue is a mantainance overkill that you might regret in the future, are you sure that’s the best way to do it?