Bug #92001 Django MySQL Python Connector returns None when Default In
Submitted: 13 Aug 2018 23:09
Reporter: Ciaran O'Sullivan Email Updates:
Status: Open Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.0.12 OS:Any
Assigned to: CPU Architecture:Any
Tags: Django, MySql Connector, python

[13 Aug 2018 23:09] Ciaran O'Sullivan
Description:
Hi,

I was working down through the Django Polls Tutorial and when I hit part 4 I get a persistent error.

TypeError at /polls/1/vote/
unsupported operand type(s) for +=: 'NoneType' and 'int'
Request Method:	POST
Request URL:	http://127.0.0.1:8000/polls/1/vote/
Django Version:	2.1
Exception Type:	TypeError
Exception Value:	
unsupported operand type(s) for +=: 'NoneType' and 'int'
Exception Location:	C:\<path>\polls\views.py in vote, line 40
Python Executable:	C:\<path>\venv\Scripts\python.exe
Python Version:	3.6.5

Whenever I have a column that has Zero for a value it seems to be returning None instead of Zero.

How to repeat:
Django URL: https://docs.djangoproject.com/en/2.1/intro/tutorial04/

We define Some Models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

We then create some rows, that have 0 for the votes column in Choice.
Whenever the value of votes is 0 it returns None instead of 0.
If I update the table with SQL it is the same problem. 

Suggested fix:
It is easy to replicate, but rather annoying to troubleshooting when new to Django in general. I was able to workaround the issue, by setting 'use_pure':True in the settings.py of Django.

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': '<set>',
        'USER': '<set>',
        'PASSWORD': '<set>',
        'HOST': 'localhost',
        'PORT': '3309',
        'OPTIONS': { 'sql_mode': 'STRICT_TRANS_TABLES', 'use_pure':True },
    }
}
[13 Aug 2018 23:16] Ciaran O'Sullivan
Example of not using 'use_pure':True

Attachment: 2018-08-14 00_16_06-127.0.0.1_8000_polls_1_results_.png (image/png, text), 24.32 KiB.

[13 Aug 2018 23:17] Ciaran O'Sullivan
Example of using 'use_pure':True

Attachment: 2018-08-14 00_17_11-127.0.0.1_8000_polls_1_results_.png (image/png, text), 22.84 KiB.