Milano
Milano

Reputation: 18735

Django-admin: MoneyField doesn't show currency

I have a model Product and model Price. The Price has a ForeignKey(Product...) and original_price and eur_price which are MoneyField's (Django-money). So one Product object can have multiple Price objects related.

I tried to inline the Price objects into Product model admin which works correctly, but when I set original_price and eur_price to be readonly_fields, it shows amounts but not currencies.

This is without making them readonly:

class PriceInline(admin.TabularInline):
    model = Price
    max_num = 10
    #readonly_fields = ('original_price','eur_price')


class ProductAdmin(admin.ModelAdmin):
    inlines = [ScanInline,]

enter image description here

And this with readonly:

class PriceInline(admin.TabularInline):
    model = Price
    max_num = 10
    readonly_fields = ('original_price','eur_price')


class ProductAdmin(admin.ModelAdmin):
    inlines = [ScanInline,]

enter image description here

Do you have any idea how to show currency there if those fields are readonly?

Upvotes: 1

Views: 600

Answers (2)

nik_m
nik_m

Reputation: 12086

Why not something like this:

class PriceInline(admin.TabularInline):
    model = Price
    max_num = 10
    readonly_fields = ('get_original_price','get_eur_price')

    def get_original_price(self, obj):
        return mark_safe('€{}'.format(obj.original_price))

    def get_eur_price(self, obj):
        return mark_safe('€{}'.format(obj.eur_price))

Upvotes: 1

Aayushi
Aayushi

Reputation: 73

Yes this happens if you do it in admin. Can you instead try to override the form?

class PriceInline(admin.TabularInline):
    model = Price
    max_num = 10

    def get_form(self, request, obj=None, **kwargs):
        form = super(PriceInline, self).get_form(request, obj, **kwargs)
        form.base_fields['original_price'].disabled = True

        return form

Upvotes: 0

Related Questions