0

One of my models has a 'status' field which is only ever modified in code. It is an integer from 1 to 6 (although this may change in the future).

However, in the Admin site, I would like to display a label for this data. So, instead of displaying '5', I would like it to say 'Error'. This means I would be able to easily filter the objects in the database that have the status 'Error' and also my colleagues who don't know what each status means as they are not involved in coding, can use the admin site to its full.

I don't know if I am going the right way about this or if it is even possible, but I would appreciate any help you can give. I would rather not change how the status is stored as it would require a big re-write of some parts of our system. In hindsight I guess it was a bad way to do it, but I didn't think the field would matter as much as it does.

danpalmer
  • 2,163
  • 4
  • 24
  • 41

2 Answers2

4

Consider to use choices. Anyway you can customize lots of things in django-admin, just read the docs:

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

alex vasi
  • 5,304
  • 28
  • 31
  • I forgot that the items listed in 'list_display' can be functions in a model as well as fields. I have added the correct display now. However it seems that 'list_filter' needs to have a field and cannot use a function. – danpalmer Jul 20 '10 at 22:38
  • I have checked the documentation and as I suspected, it must have fields and it only supports some types. Unfortunately, the search doesn't even support functions either. – danpalmer Jul 20 '10 at 22:41
  • That's why you should have used choices in the first place. You really have to try it. (As a last resort see http://stackoverflow.com/questions/991926/custom-filter-in-django-admin) – alex vasi Jul 20 '10 at 22:53
0

You could create a custom field type that overrides to_python and get_prep_value to store integers in the db but use string values within Python:

http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.django.db.models.Field

Amber
  • 507,862
  • 82
  • 626
  • 550
  • That looks like a possibility, but it still means re-writing a lot of code that knows the numbers. Ideally, I want them to still be numbers everywhere except the Admin site. I was thinking it might be similar to adding a human readable label to something in the Admin site. – danpalmer Jul 20 '10 at 22:26
  • But it is much nicer, cleaner, quicker, code for 90% of the time. It is just when you get to this that it gets annoying. I think in Ruby you have symbols which are used commonly for this sort of thing. – danpalmer Jul 20 '10 at 22:46