'How to display $ (dollar signs) in Django Admin?

How can I make it so under my price variable I can automatically have it display a $? So, instead of having 999.99, it will display $999.99. I am doing this in Django Admin.

Here is admin.py

from django.contrib import admin
from purchaseorders.models import PurchaseOrder


class PurchaseOrderAdmin(admin.ModelAdmin):
    fields = ['product', 'price', 'purchase_date', 'confirmed']
    list_display = ('product', 'price', 'purchase_date', 'confirmed', 'po_number')
admin.site.register(PurchaseOrder, PurchaseOrderAdmin)

And here is the bit from models.py

from django.db import models
import random

class PurchaseOrder(models.Model):
    price = models.FloatField()


Solution 1:[1]

You can do it in a couple of ways:

Add a custom property in PurchaseOrder

class PurchaseOrder(models.Model):
    price = models.FloatField()

    @property
    def dollar_amount(self):
        return "$%s" % self.price if self.price else ""

and reference dollar_amount instead of price

Another way

Add it in the PurchaseOrderAdmin

class PurchaseOrderAdmin(admin.ModelAdmin):
    fields = ['product', 'price', 'purchase_date', 'confirmed']
    list_display = ('product', 'dollar_amount', 'purchase_date', 'confirmed', 'po_number')

    def dollar_amount(self, obj):
        return "$%s" % obj.price if obj.price else ""


admin.site.register(PurchaseOrder, PurchaseOrderAdmin)

I would personally prefer option 1 so that we could reuse the code if the same has to be done with the actual app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1