0

Let's assume I have the following :

models.py :

VALUE_CHOICES = (('0', 'ZERO'),
                 ('1', 'ONE'))

class ModelA(models.Model):
    def val(self):
        return self.model_b.value or ''

class ModelB(models.Model):
    modela = models.OneToOneField(ModelA, related_name="model_b", null=False)
    value = models.CharField(choices = VALUE_CHOICES)

and

admin.py :

class ModelAAdmin(ModelAdmin):
    list_display = ['val']

When I try to display my ModelA list in the admin site it displays 0 or 1 and not ZERO or ONE.

How could I modify this to make it display the human readable name from VALUE_CHOICES in the list from the admin site?

vmonteco
  • 14,136
  • 15
  • 55
  • 86

1 Answers1

1

From the docs, and for the field val,

class ModelAAdmin(ModelAdmin):
    list_display = ['get_val_display']

This should do the job.

[EDIT by OP] I didn't succeed to make it work like this but this kind of method was a really good suggestion. Here is how I used this get_FOO_display() method (and it worked like a charm).

I only modified my ModelA.val() method :

def val(self):
    return self.model_b.get_value_display()
vmonteco
  • 14,136
  • 15
  • 55
  • 86
alioguzhan
  • 7,657
  • 10
  • 46
  • 67
  • @vmonteco sorry my bad. I thougt `val` is a regular field. Didn't notice that it is a method :) – alioguzhan May 16 '16 at 02:00
  • 1
    No problem! the get_foo_display() was the only thing I really needed. I also tried with `list_display = ['movel_b__get_value_display']` though but it didn't work either. :) – vmonteco May 16 '16 at 10:18
  • `list_display = ['model_b__get_value_display']`* sorry – vmonteco May 16 '16 at 10:59