简体   繁体   中英

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,]

在此处输入图片说明

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,]

在此处输入图片说明

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

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM