【问题标题】:Django equivalent for MySQL bit datatypeMySQL 位数据类型的 Django 等效项
【发布时间】:2019-07-19 21:07:01
【问题描述】:

我的数据库(MySQL)中的“makeflag”字段是位(1)类型,我遇到了问题。我尝试使用以下语法的 booleanField 和 bit1booleanfield。但我对两者都有错误。当我在这个模型上尝试使用 json 数据的 POST 请求时,

我得到错误

将 1 或 0 作为值传递时“列的数据太长”。

当我给出 true 或 false 作为值时,我会得到 400 Bad Request。 有人可以帮我理解如何使用 django 和 json 为位字段(mysql)发布数据。

makeflag=models.BooleanField(db_column='MakeFlag', default=1)

makeflag=Bit1BooleanField()

我的模特是下一个:

class Product(models.Model):
    productid = models.AutoField(db_column='ProductID', primary_key=True)  
    name = models.CharField(db_column='Name', max_length=50)  
    productnumber = models.CharField(db_column='ProductNumber', max_length=25)  
    makeflag = models.TextField(db_column='MakeFlag', max_length=1)
    color = models.CharField(db_column='Color', max_length=15, blank=True)
    safetystocklevel = models.SmallIntegerField(db_column='SafetyStockLevel')  
    reorderpoint = models.SmallIntegerField(db_column='ReorderPoint')  
    standardcost = models.FloatField(db_column='StandardCost') 

【问题讨论】:

  • 请添加更多与您遇到的错误相关的信息?您如何使用 JSON 发布数据?
  • 嗨,我的示例 json 如下 { "name": "ramram", "productnumber":"1", "makeflag":1, "color":"Blue", "safetystocklevel" :"1000", "reorderpoint":"750", "standardcost":"0"}
  • 为什么要将"1000", "750", "0" 之类的字符串发送到safetystocklevel、reorderpoint 和standardcost SmallIntegerFields。这些字段必须是整数,不能是字符串...
  • 我正在使用邮递员发布数据
  • 错误:文件 "e:\anaconda\lib\site-packages\mysql\connector\cursor_cext.py" 在执行 raw_as_string=self._raw_as_string) 文件 "e:\anaconda\lib\site- packages\mysql\connector\connection_cext.py" in cmd_query sqlstate=exc.sqlstate 在处理上述异常期间(Data too long for column 'MakeFlag' at row 1),发生另一个异常:文件“e:\anaconda\lib\ site-packages\mysql\connector\cursor_cext.py" in statement return self._executed.strip().decode('utf8') 在处理上述异常('NoneType'对象没有属性'strip'),另一个异常发生:

标签: mysql django boolean


【解决方案1】:

您可能需要使用django-mysql 来实现mysql 的特定功能。看看here 的文档

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 2010-11-13
    • 1970-01-01
    • 2010-09-30
    • 2021-03-06
    • 1970-01-01
    • 2023-04-08
    • 2014-07-14
    相关资源
    最近更新 更多