使用mysql在django模型charfield中获取空白语句

发布于 2024-09-29 11:51:40 字数 381 浏览 5 评论 0原文

我刚刚将 django 应用程序从 sqlite3 迁移到 mysql 5.1.41。我在模型中有一个像这样定义的 charfield:

class HostData(models.Model):
  Host = models.CharField(max_length=50, null=True)
  HostStatus = models.CharField(max_length=200, null=True)
  Alarm = models.BooleanField()

除了 HostStatus 之外,一切都一样,它通常返回一个类似“Up, waiting”的字符串。然而在 mysql 中,它是空白的。我创建了字符集为 utf-8 的表。我做错了什么?提前致谢。

I just migrated my django application from sqlite3 to mysql 5.1.41. I have a charfield in a model defined like this:

class HostData(models.Model):
  Host = models.CharField(max_length=50, null=True)
  HostStatus = models.CharField(max_length=200, null=True)
  Alarm = models.BooleanField()

Everything works the same except HostStatus, which usually returns a string like "Up, waiting". In mysql, however, it is blank. I created my table with character set to utf-8. What am I doing wrong? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-10-06 11:51:40

我看不出该模型代码有什么问题。

您是如何将数据迁移到 MySQL 的?当您通过 django admin 输入数据时,它是否将数据输入到数据库中?

如果视图正在输入此数据,您可以发布视图代码吗?

I can't see anything wrong with that model code.

How have you migrated your data over to MySQL? When you enter data via the django admin, does it enter data into the db?

If a view is entering this data, can you post the view code.

七婞 2024-10-06 11:51:40

问题在于我为模型分配值的方式。我不知道为什么它在 sqlite 中运行良好,但无论如何它都是糟糕的编码。这就是我正在做的:

myarray = stdout_ssh
myobject = test.object.get(id=3)
myobject.Host = stdout_ssh[0]
myobject.HostStatus = status
status = stdout_ssh[1]
myobject.Alarm = False
myobject.save()

我将其更改为:

myarray = stdout_ssh
myobject = test.object.get(id=3)
myobject.Host = stdout_ssh[0]
myobject.HostStatus = stdout_ssh[1]
myobject.Alarm = False
myobject.save()

一切都很好。

The issue was the way I was assigning the value to the model. I dont know why it worked well in sqlite, but it was bad coding anyways. This is what I was doing:

myarray = stdout_ssh
myobject = test.object.get(id=3)
myobject.Host = stdout_ssh[0]
myobject.HostStatus = status
status = stdout_ssh[1]
myobject.Alarm = False
myobject.save()

I changed this to:

myarray = stdout_ssh
myobject = test.object.get(id=3)
myobject.Host = stdout_ssh[0]
myobject.HostStatus = stdout_ssh[1]
myobject.Alarm = False
myobject.save()

And everything was ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文