Django admin question: correct display of choices field in list_display
In my django admin, I'm trying to configure a custom column display:
My model :
class Batch(models.Model):
WEIGHT_UNIT = (
(1, 'G'),
(1000, 'K'),
(1000 * 100, 'Q'),
(1000 * 1000, 'T')
)
batch_number = models.CharField(max_length=12, unique=True)
available_quantity_value = models.DecimalField(max_digits=10, decimal_places=3)
available_quantity_unit = models.IntegerField(choices=WEIGHT_UNIT)
My model admin :
class BatchAdmin(forms.ModelAdmin):
list_display = ('batch_number', 'available_quantity_value', 'available_quantity_unit')
If I do this, I have two columns in my admin for the batches list page, and the second columns displays the value taken from the WEIGHT_UNIT constant (K, G, etc.)
Now, I would like to have one single column, displaying something like "10K", "250G",开发者_如何学C etc. So here's my new model admin :
class BatchAdmin(forms.ModelAdmin):
list_display = ('batch_number', 'get_available_quantity')
and in my model :
def get_available_quantity(self):
return '%s%s' % (self.available_quantity_value, self.available_quantity_unit)
get_available_quantity.shirt_description = _('Available quantity')
But what I got in my column is something like "101000", "2501", etc. How can I tell django to display the value taken from the choices option?
get_available_quantity_unit_display()
精彩评论