Django's IntegerField retrieved as None despite being 0

Hi there! I'm facing something really strange. I have a record value in the database which MySQL shows being zero, while the Django ORM retrieves it as None. Check out:

mysql> select * from mytable;
| id | number | start_date | expiration_date |
|  1 |      0 | 2019-08-08 | 2020-01-15      |
1 row in set (0.00 sec)

The column number is clearly zero, but Django's shell says...

In [3]: record = models.MyTable.objects.get(id=1)
In [4]: record.number is None
Out[4]: True

The field definition is a simple:

number = models.IntegerField()

The same code running in my local machine shows record.value == 0. I've checked that Django, mysql-connector-python and MySQL are all the same version as in my local machine, same table definition, no migrations pending.

Any ideas?

Thanks in advance!

hmm. did you at any point have blank= or null= type settings in your Django db models? Also if you start a new shell now, does the new mysql select and the new django shell still give that result?

Just to double check, the mysql that you are selecting from is the pythonanywhere one right? and also your pythonanywhere shell is connected to the correct pythonanywhere mysql databse right?

Hi conrad, thanks for the answer. Both Django and MySQL are still showing different results (None and 0). I'm almost sure the field was never null=True nor blank=True, however, based on show create table mytable, the configuration at database level seems to be fine:

`number` int(11) NOT NULL

I've just confirmed that switching the bakend to django.db.backends.mysql makes Django retrieve the right value for number. Maybe a bug in mysql.connector.django? However is still working in my local machine (although Windows) with the same driver and MySQL version...

Interesting! I googled for a few appropriate terms, and someone else did report a bug that sounds like this to the MySQL-connector team back in 2018, though it doesn't look like anyone picked it up. So you may be right -- though I agree it's odd that you don't see the same problem on your local machine; the library is pure Python so you'd expect it would behave the same way on both platforms.

Thanks giles! It seems like the same issue, and at least provides a partial solution by adding 'use_pure': True. However I'm going ahead with mysqlclient which seems to work fine and is the recommended backend.

Right, that's probably the best solution. There was a time a few years ago when the normal MySQL library for Python wasn't compatible with Python 3.x, and mysqlconnector was the best option, but now that mysqlclient exists and works well with 3.x, we'd recommend using it. It's what we use for all of our own MySQL stuff internally.