1

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?

Milano
  • 18,048
  • 37
  • 153
  • 353

2 Answers2

1

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))
nik_m
  • 11,825
  • 4
  • 43
  • 57
0

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
Aayushi
  • 73
  • 1
  • 8