[Solved]-Django: override get_FOO_display()

10👍

Normally you would just override a method as you have shown. But the trick here is that the get_FOO_display method is not present on the superclass, so calling the super method will do nothing at all. The method is added dynamically by the field class when it is added to the model by the metaclass – see the source here (EDIT: outdated link as permalink).

One thing you could do is define a custom Field subclass for your unit field, and override contribute_to_class so that it constructs the method you want. It’s a bit tricky unfortunately.

(I don’t understand your second question. What exactly are you asking?)

1👍

Now in Django > 2.2.7:

Restored the ability to override get_FOO_display() (#30931).

You can override:


    class FooBar(models.Model):
        foo_bar = models.CharField(_("foo"),  choices=[(1, 'foo'), (2, 'bar')])
    
    
   
        def get_foo_bar_display(self):
            return "something"

0👍

You could do it this way:

  1. Override the Django IntegerField to make a copy of your get_FOO_display function:

    class MyIntegerField(models.IntegerField):
        def contribute_to_class(self, cls, name, private_only=False):
            super(MyIntegerField, self).contribute_to_class(cls, name, private_only)
            if self.choices is not None:
                display_override = getattr(cls, 'get_%s_display' % self.name)
                setattr(cls, 'get_%s_display_override' % self.name, display_override)
    
  2. In your class, replace your choice field with MyIntegerField:

    class A(models.Model):
       unit = MyIntegerField(choices=something)
    
  3. Finally, use the copy function to return the super value:

       def get_unit_display(self, value):
           if your condition:
              return your value
           return self.get_unit_display_override()
    
👤gapson

0👍

You can’t directly call super() because the original method doesn’t "exist" yet on the parent model.

Instead, call self._get_FIELD_display() with the field object as its input. The field object is accessible through the self._meta.get_field() method.

def get_unit_display(self):
    singular = self._get_FIELD_display(self._meta.get_field('unit'))
    return singular + 's'

-4👍

You should be able to override any method on a super class by creating a method with the same name on the subclass. The argument signature is not considered. For example:

class A(object):
    def method(self, arg1):
        print "Method A", arg1

class B(A):
    def method(self):
        print "Method B"

A().method(True) # "Method A True"
B().method() # "Method B"

In the case of get_unit_display(), you do not have to call super() at all, if you want to change the display value, but if you want to use super(), ensure that you’re calling it with the correct signature, for example:

class A(models.Model):
    unit = models.IntegerField(choices=something)

    def get_unit_display(self, value):
        display = super(A, self).get_unit_display(value)
        if value > 1:
            display = display + "s"
        return display

Note that we are passing value to the super()’s get_unit_display().

Leave a comment