On Monday 19 June 2017 04:00:42 Priyanka Thakur wrote:
> On Monday, 19 June 2017 16:05:06 UTC+5:30, m712 - Developer wrote:
> > Melvyn was not saying whether you were checking for None. If `value`
> > is i.e. "some string" your to_python method will return `None`. You
> > should do something like this:
> > 
> > ```
> > 
> >     if value in ('t', ...): return True
> >     elif value in ('f', ...): return False
> >     else: return bool(value) # You can change this
> > 
> > ```

That's correct for to_python. But personally, I would raise an exception in 
from_db_value, since 
that indicates data corruption.

In your case, there are only a handful of allowed values that should come from 
the database. 
Anything else, will indicate somebody messing with your data or upgrades of one 
of the 
libraries in between Django and the database changed it's output format. Both 
of which will 
require attention.

This is one advantage of from_db_value and to_python separation.

-- 
Melvyn Sopacua

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2543772.HvS691Qiqz%40devstation.
For more options, visit https://groups.google.com/d/optout.

Reply via email to