Django queryset对象返回None而不是0,即使数据库将字段值存储为0 [英] Django queryset objects return None instead of 0 even though the database has 0 stored as the field value

查看:833
本文介绍了Django queryset对象返回None而不是0,即使数据库将字段值存储为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始为我的Django项目使用mysql-connector/python而不是mysqlclient时遇到问题.突然,在结果查询集中,数据库中存储为0的值将变为"None",这是一个示例.

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"

如果使用mysqlclient,同一条语句将显示0.

Where the same statement would have printed 0 with mysqlclient.

是否有解决此问题的好方法?我还没有获得mysqlclient来与当前的设置很好地配合,并且宁愿不必根据0值跟踪所有调用并对其进行重组以处理Nonetypes.

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.

使用相关模型进行

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)

已售,已付费和已批准全部返回为无",即使可以使用0作为值来查询它们.

Sold, Paid, and Approved all come back as None, even though they can be queried using 0 as a value.

其他示例:

my_foo = Foo(bar=0)
my_foo.save()
print(my_foo.bar)  # prints 0

使用mysql-connector-python进行单独的查询会在查询集中产生0.

Separate queries using mysql-connector-python yields 0 as expected in the queryset.

Django版本2.0.3.0,mysql-connector-python版本8.0.12.

Django version 2.0.3.0, mysql-connector-python version 8.0.12.

推荐答案

如果仍然有人遇到此问题(像我一样),我找到了一个对我有用的解决方案.在settings.py文件的数据库配置中,添加以下选项:

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:

'OPTIONS': {'use_pure': True }

这为我解决了问题. 希望这会有所帮助.

This resolved the problem for me. Hope this helps.

这篇关于Django queryset对象返回None而不是0,即使数据库将字段值存储为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆