How to preprocess a Django model field value before return?
- by Satoru.Logic
Hi, all.
I have a Note model class like this:
class Note(models.Model):
author = models.ForeignKey(User, related_name='notes')
content = NoteContentField(max_length=256)
NoteContentField is a custom sub-class of CharField that override the to_python method in purpose of doing some twitter-text-conversion processing.
class NoteContentField(models.CharField):
__metaclass__ = models.SubfieldBase
def to_python(self, value):
value = super(NoteContentField, self).to_python(value)
from ..utils import linkify
return mark_safe(linkify(value))
However, this doesn't work.
When I save a Note object like this:
note = Note(author=request.use,
content=form.cleaned_data['content'])
The conversed value is saved into the database, which is not what I wanna see.
Would you please tell me what's wrong with this?
Thanks in advance.