2017-02-13 116 views
0

我有一個模型Product和模型PricePrice具有ForeignKey(Product...)original_priceeur_price,它們是MoneyField's(Django-money)。所以一個Product對象可以有多個相關的Price對象。Django-admin:MoneyField不顯示貨幣

我試圖將Price對象內嵌到Product模型管理員,它可以正常工作,但是當我將original_price和eur_price設置爲readonly_fields時,它顯示金額而非貨幣。

這是沒有讓他們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

這與只讀:

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

你有任何想法如何顯示貨幣有如果這些字段是隻讀?

回答

0

是的,如果你在管理員中這樣做,會發生這種情況。你可以嘗試重寫表單嗎?

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 
0

爲什麼不能是這樣的:

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