Bug #92001 Django MySQL Python Connector returns None when Default In
Submitted: 13 Aug 2018 23:09 Modified: 28 Mar 2021 2:56
Reporter: Ciaran O'Sullivan Email Updates:
Status: No Feedback Impact on me:
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

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:
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.

    '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- (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- (image/png, text), 22.84 KiB.

[15 Aug 2019 13:12] Matthew Vigliotti
I just ran into this myself and I can confirm this is still a problem with version 8.0.17. Not surprising I guess since this bug submission seems to be untouched since submitted a year ago...
[31 Oct 2019 7:51] Dima German
The bug is still there in version 8.0.18.
Your workaround still helps.

Django dev team found it, too: https://code.djangoproject.com/ticket/30469
[17 Dec 2019 16:42] Nuno Mariz
I've tested with Django 2.1.8 and Connector/Python 8.0.12, 8.0.16 and 8.0.18, but I couldn't reproduce the error.

Can you please provide a full traceback?

[28 Feb 2021 2:56] MySQL Verification Team
Please check version 8.0.23.
[1 Apr 2021 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".