Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要编写一个 Django 自定义字段,其中存储的值未完全显示。例如,如果存储的值为“079684”,我需要将其显示为“---84”。有可能这样做吗?如果是,我应该如何实现我的自定义字段?
我通过覆盖自定义字段方法的 from_db_value() 解决了这个问题。
您可以将值作为字符串存储在模型中,然后在模板中使用切片以仅显示最后两个字符。
{{ value|slice:"-2:" }}