I encountered a problem when I started using mysql-connector/python instead of mysqlclient for my Django project. Suddenly values stored as 0 in the database is turned to "None" in the resulting queryset, here's an example.
queryset = Invoices.objects.all().filter(sold=0)
print(queryset[0].sold) # Prints "None"
Where the same statement would have printed 0 with mysqlclient.
Is there a good way to fix this? I haven't gotten mysqlclient to work well with my current setup and would rather not have to track down all the calls depending on a 0-value and restructure it to handle Nonetypes.
Edit with the model in question:
from django.db import models
from authentication.models import Account, SubUser
class Invoices(models.Model):
rating = models.CharField(max_length=5, null=True)
serial = models.CharField(max_length=60, null=False)
amount = models.CharField(max_length=60, null=False)
debtor = models.CharField(max_length=128, null=False)
debtorname = models.CharField(max_length=128, null=False)
seller = models.ForeignKey(Account, on_delete=models.DO_NOTHING, related_name='soldinvoices', null=False)
dateout = models.CharField(max_length=256, null=False)
expiration = models.CharField(max_length=256, null=False)
buyer = models.ForeignKey(Account, on_delete=models.DO_NOTHING, related_name='boughtinvoices', null=True)
fid = models.CharField(max_length=256, null=False)
filepath = models.CharField(max_length=256, null=False)
approved = models.IntegerField(default=0)
paid = models.IntegerField(default=0)
sold = models.IntegerField(default=0)
date = models.DateTimeField(auto_now_add=True, null=False)
subseller = models.ForeignKey(SubUser, null=True, on_delete=models.DO_NOTHING)
askingprice = models.CharField(max_length=100, null=True)
Sold, Paid, and Approved all come back as None, even though they can be queried using 0 as a value.
Additional example:
my_foo = Foo(bar=0)
my_foo.save()
print(my_foo.bar) # prints 0
Separate queries using mysql-connector-python yields 0 as expected in the queryset.
Django version 2.0.3.0, mysql-connector-python version 8.0.12.
If anyone is still running into this problem (as I did), I found a solution that worked for me. In the database configuration in the
settings.py
file, add the following option:This resolved the problem for me. Hope this helps.
pass null argument to all those fields.