Django: override get_FOO_display()

前端 未结 4 1788
無奈伤痛
無奈伤痛 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条回答
  • 2021-01-02 03:46

    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 讨论(0)
  • 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?)

    0 讨论(0)
  • 2021-01-02 03:51

    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().

    0 讨论(0)
  • 2021-01-02 04:00

    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()
      
    0 讨论(0)
提交回复
热议问题