Django: right-align column in admin listview - django

How can I output a DecimalField right-aligned in Django admin listview? It makes much more sense to have a column of, say, prices, right-aligned.
I tried this: django align right cannot align, but it doesn't work, presumably, because it doesn't change the output behaviour.

Related

Django model fields get realigned after writing wrong data

In my Django admin panel, whenever I leave a field blank and save it the fields are realigned left to right and vice versa.
In the below pics 1st pic is when there is no data in the fields. 2nd pic is when i leave a field blank and save it the field has realigned from left to right.
Can anyone tell me what is the problem and how to fix this?
![Before writing anything in my panel[![After leaving a field blank][1]][1]][2]
[![After leaving a field blank][1]][1]

Django admin site column headers sort by values not alphabetical?

How do you make Django admin site to strictly sort by values alphabetically when column headers are clicked? column2 below is a foreignkey.
MyAdmin(admin.ModelAdmin):
list_display = ('column1', 'column2')
Here column2 header when clicked should sort the change list alphabetically but is not?
column2
b
a
c
or
column2
c
a
b
is the output.
The information you provided (up to this moment) is not enough to give you a straight answer. However based on what you say, I suspect the problem you are facing is the following:
Django Admin sorts the "Strings" alphabetically (asc or desc depending of the current state), but when the column is a foreign-key it sorts by the id, even though it is displaying a "String" that you specified by the __str__(self): method on your model.
Since the sorting is done at the database, based on my knowledge you will not be able to do it, without editing the queryset (check this answer to see an example: How to allow sorting in the Django admin by a custom list_display field, which doesn't have a DB field nor annotatable).
This should be the default behaviour. Perhaps you have clicked on another column header before and then it adds column2 as the second ordering parameter? You can check the admin URL and see what ordering parameters are given (and if you don't know how to make sense of the URL, you can paste it here as well).
The other version I can think of is that the rows values are not in fact a, b and c, but some unicode stuff that isn't ordered correctly by default (i.e. usually a, b, ä are usually by default sorted a, ä, b when in fact in Estonian alphabet the first ordering is the correct one). To solve this problem, checkout the collation options on Django models.

Can I disable (not remove) a particular row in a django-admin result?

I am using dfrdmn's neat hack to add a summary row to a Django admin view. Briefly, it overrides the ChangeList for the ModelAdmin to append a new item to self.result_list._result_cache
However, this row is treated like other rows in that:
it has a selection box (for performing actions on it)
it has clickable link fields (for opening the edit view)
it has editable fields (for same-page edits)
it is considered part of the result_list when clicking Save (which results in an error)
How can I disable all of these so that the row is view-only, not selectable and ignored when saving?
As a bonus, displaying nothing for the "Like" field would be nice but is a low priority. (It's a NullBooleanField.)

Checkbox layout in Django manager

A minor but rather annoying flaw in the default layout of forms in Django manager apps is the positioning of checkboxes where the checkbox and its label are positioned in the first column and its help text (if set) is aligned in the second column:
Either the checkbox and label should be positioned in the second column or -better- the label should be aligned in the first column (with all other labels) and the checkbox positioned in the second column.
I presume the chances are slim, but is there a relatively easy fix for this?
If you can do it by adding css, then it is relatively easy:
class MyModel(admin.ModelAdmin):
class Media:
css = {
'all': ('/path/to/css.css',)
}

How can display a FloatField in a Django admin column rounded to the nearest integer, without using a custom list_display?

It would be easy with a custom list_display, but that would cause me to lose the sorting ability according to that column.
The docs say this:
However, if an element of list_display represents a certain database field, you can indicate this fact by setting the admin_order_field attribute of the item.
So you should define your function to display the rounded value, but set admin_order_field to preserve the ordering capability.