Django IntegerField retrieves a 0 as a None value when using MySQL

plugcity :

Django has started retrieving all of my IntegerField with a value of zero as None instead of as a zero.

This is happening across all of my models. I recently updated mysql-connector-python so I am not sure if that could be the problem.

Does anyone know of a global setting or bug that would cause Django to start doing this across all of my models?

example:

class Brands(models.Model):
   brand_id = models.AutoField(primary_key=True)
   brand_name = models.CharField(unique=True, max_length=512)
   blacklisted = models.IntegerField()
   blacklisted_date = models.DateTimeField(blank=True, null=True)
   date_created = models.DateTimeField()

   class Meta:
       managed = False
       db_table = 'brands'

The values for blacklisted are all being retrieved as None instead of 0. They are stored as a 0 in the database.

Django version 2.2.1

MySQL InnoDB

Thanks for any help you can provide.

Azy_Crw4282 :

A workaround for this bug: add 'use_pure': True to database OPTIONS. This forces mysql-connector-python to use pure python connection instead of C extension, where I believe the bug lives, see here

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': os.environ['MYSQL_DATABASE'],
        'USER': os.environ['MYSQL_USER'],
        'PASSWORD': os.environ['MYSQL_PASSWORD'],
        'OPTIONS': {
            'use_pure': True,
    }
}

Or alternatively you can set this in the settings.py file

'OPTIONS': {'use_pure': True }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5615&siteId=1