Custom field in Django get_prep_value() has no effect

jvc*_*c26 3 django django-models

So, I've made a similar class to this answer. It looks like:

class TruncatingCharField(models.CharField):
    description = _("String (truncated to %(max_length)s)")

    def get_prep_value(self, value):
        value = super(TruncatingCharField, self).get_prep_value(value)
        if value:
            value = value[:self.max_length]
        return value
Run Code Online (Sandbox Code Playgroud)

I would expect that instantiating a model with this field with strings longer than the threshold should trigger truncation. However this appears not to be the case:

class NewTruncatedField(models.Model):
    trunc = TruncatingCharField(max_length=10)


class TestTruncation(TestCase):

    def test_accepted_length(self):
        trunc = 'a'*5
        obj = NewTruncatedField(trunc=trunc)
        self.assertEqual(len(obj.trunc), 5)

    def test_truncated_length(self):
        trunc = 'a'*15
        obj = NewTruncatedField(trunc=trunc)
        self.assertEqual(len(obj.trunc), 10)
Run Code Online (Sandbox Code Playgroud)

The first test passes, as would be expected, but the second fails, as the field does not truncate its value. The get_prep_value() method is definitely being called (tested via breakpoints), and the output of value at the point of return is correctly truncated.

Why then is the value of the field in the obj object not truncated?

DBr*_*wne 5

我相信get_prep_value()只会影响保存到数据库的内容,而不是 Python 对象的内部值。

尝试覆盖to_python()和移动你的逻辑。