Django: override get_FOO_display()

前端 未结 4 1807
無奈伤痛
無奈伤痛 2021-01-02 03:16

In general, I\'m not familiar with python\'s way of overriding methods and using super().

question is: can I override get_FOO_display()?



        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 03:48

    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?)

提交回复
热议问题