Friday, June 16, 2017

Re: Custom field implementing Boolean Field is having response value as None instead of False

On Friday 16 June 2017 07:33:22 Priyanka Thakur wrote:

> Hi,

>

> I am doing migration from Django 1.7 to 1.10 version and has a custom

> django field.

>

> Below is the custom class for the field:

>

> ----

>

> from django.db import models

>

>

>

> class HibernateBooleanField(models.BooleanField):

>

> def from_db_value(self, value, expression, connection, context):

>

> return self.to_python(value)

>

>

> def get_internal_type(self):

>

> return "HibernateBooleanField"

>

>

> def db_type(self, connection):

>

> return 'bit(1)'

>

>

> def to_python(self, value):

>

> if value in (True, False):

>

> return value

>

> if value in ('t', 'True', 'true', '1', '\x01'): return True

>

> if value in ('f', 'False', 'false', '0', '\x00', None): return

> False

 

and if value is something else, nothing (aka None) is returned. You have no else clause here.

 

--

Melvyn Sopacua

No comments:

Post a Comment