How to preprocess a Django model field value before return?
Posted
by Satoru.Logic
on Stack Overflow
See other posts from Stack Overflow
or by Satoru.Logic
Published on 2010-04-04T03:19:46Z
Indexed on
2010/04/04
3:23 UTC
Read the original article
Hit count: 305
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.
© Stack Overflow or respective owner