django中list_editable的千位分隔符?

时间:2015-09-22 11:39:37

标签: django django-admin

* django中的list_editable是否有一千个分隔符(如1,000)?

#model.py
class Product(models.Model):
deal_price = models.IntegerField(db_index=True, blank=True, null=True, default=0, validators=[position], verbose_name=_("Current Price"))

-//-

class StockProduct(Product):
    category = models.ForeignKey(to=StockCategory, related_name='stock_products')
    warehouse = models.ForeignKey(to=WareHouse, blank=True, null=True)

    objects = ProductManager()

#admin.py
class StockProductAdmin(admin.ModelAdmin):
    form = StockProductAdminForm
    list_display = ('product_id', 'title', 'quantity', 'category', 'deal_price_sep')
    list_editable = ('quantity', 'deal_price_sep')

def deal_price_sep(self, instance):
        return '{0:,}'.format(self.deal_price)

admin.site.register(models.StockProduct, StockProductAdmin)

每当我将deal_price_sep更改为deal_price时,它都有效。但我希望list_editable显示价格为千分隔符。

2 个答案:

答案 0 :(得分:0)

我还没有测试过这个,但我认为你应该可以做到:

class PersonAdmin(admin.ModelAdmin):
    list_editable = ('foo_thousand',)
    list_display = ('foo_thousand',)

    def foo_thousand(self, instance):
        return '{0:,}'.format(self.foo)

但是,在保存之前,您可能需要删除不是数字的字符。您可以使用save_model()方法执行此操作。

答案 1 :(得分:0)

如果有人还想显示更大的数字:

添加到设置文件中的默认设置(例如settings.py):

USE_THOUSAND_SEPARATOR = True

Source