1

I need to write a Django custom field where the stored value is not fully displayed. For example if the stored value is "079684" I need it to be displayed as "---84". Is it possible to do so ? If yes how should I implement my custom field ?

Akamee
  • 593
  • 1
  • 6
  • 21
  • I'm not sure this is something to do at the *model* level. Typically the "presentation" of data is handled at the form, view, and/or template level. – Willem Van Onsem Nov 01 '18 at 11:06

2 Answers2

1

I have solved this by overriding from_db_value() of the custom field method.

Akamee
  • 593
  • 1
  • 6
  • 21
  • It's an option, but I would say you should consider what can be achieved in a simple way. Using normal CharField and than slicing the value in template would be in my opinion more simple than going with custom field. – Uroš Trstenjak Nov 01 '18 at 14:31
0

You could store your value as a string in your model and than use slice in template to show only last two character.

{{ value|slice:"-2:" }}
Uroš Trstenjak
  • 903
  • 8
  • 14