- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 models.py 文件中 inspectdb
中的外键定义方面遇到了许多问题。
我有一组旧表,我想将其合并到我的 Django 应用程序中,因此我运行了 python manage.pyspectdb > models.py
我想在我的应用程序中使用 7 个 GPS 表。查看其中 2 个就足以描述我遇到的问题。
MySQL建表语句:
CREATE TABLE `gps` (
`longitude_dir` char(1) DEFAULT NULL,
`longitude` decimal(8,5) DEFAULT NULL,
`latitude` decimal(7,5) DEFAULT NULL,
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`host_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`latitude_dir` char(1) DEFAULT NULL,
`host` varchar(24) NOT NULL,
`altitude_units` char(1) DEFAULT NULL,
`gps_time` datetime DEFAULT NULL,
`raw` blob NOT NULL,
`clean` bit(1) DEFAULT NULL,
`data_valid` bit(1) DEFAULT NULL,
`modem` varchar(15) NOT NULL,
`altitude` decimal(7,2) DEFAULT NULL,
PRIMARY KEY (`id`,`host`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
CREATE TABLE `gps_zda` (
`local_zone_minutes` tinyint(4) DEFAULT NULL,
`host` varchar(24) NOT NULL,
`year` tinyint(4) DEFAULT NULL,
`day` tinyint(4) DEFAULT NULL,
`month` tinyint(4) DEFAULT NULL,
`local_zone` tinyint(4) DEFAULT NULL,
`gps_id` int(10) unsigned NOT NULL,
`timestamp` time DEFAULT NULL,
KEY `gps_fk_zda` (`gps_id`,`host`),
CONSTRAINT `gps_fk_zda` FOREIGN KEY (`gps_id`, `host`) REFERENCES `gps` (`id`, `host`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
这是模型的子集
from __future__ import unicode_literals
from django.db import models
class Gps(models.Model):
longitude_dir = models.CharField(max_length=1, blank=True, null=True)
longitude = models.DecimalField(max_digits=8, decimal_places=5, blank=True, null=True)
latitude = models.DecimalField(max_digits=7, decimal_places=5, blank=True, null=True)
id = models.AutoField()
host_time = models.DateTimeField()
latitude_dir = models.CharField(max_length=1, blank=True, null=True)
host = models.CharField(max_length=24)
altitude_units = models.CharField(max_length=1, blank=True, null=True)
gps_time = models.DateTimeField(blank=True, null=True)
raw = models.TextField()
clean = models.TextField(blank=True, null=True) # This field type is a guess.
data_valid = models.TextField(blank=True, null=True) # This field type is a guess.
modem = models.CharField(max_length=15)
altitude = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
class Meta:
managed = False
db_table = 'gps'
unique_together = (('id', 'host'),)
class GpsZda(models.Model):
local_zone_minutes = models.IntegerField(blank=True, null=True)
host = models.ForeignKey(Gps, db_column='host')
year = models.IntegerField(blank=True, null=True)
day = models.IntegerField(blank=True, null=True)
month = models.IntegerField(blank=True, null=True)
local_zone = models.IntegerField(blank=True, null=True)
gps = models.ForeignKey(Gps)
timestamp = models.TimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'gps_zda'
<小时/>
1.因此,我在尝试 python manage.py makemigrations
时遇到的第一个问题是 (gps.id, gps.host)
没有正确定义为主键。事实上,似乎很多限制都被忽略了。我一开始收到一个错误,说 gps.id 被定义了两次。
改变
id = models.Autofield()
...
host = models.CharField(max_length=24)
至
id = models.Autofield(primary_key=True)
...
host = models.CharField(primary_key=True, max_length=24)
让 django 知道不要创建它自己的 id 字段。我仍然不确定它是否正确处理共享 key 。
<小时/>2.尝试运行 makemigrations 时出现了第二个问题,因为 gps_zda 表具有共享的、唯一的外键。 Django 提示它不知道如何命名第二个外键。
向其中一个外键添加 lated_name
似乎可以阻止投诉。
我变了
host = models.ForeignKey(Gps, db_column='host')
至
host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')
我再次不确定这是否正确处理共享外键。
<小时/>3.为了进行一些测试,我决定将模型添加到 admin.py 并查看字段是否正确填充。
这导致发现更多问题。
问题 3。当我打开 gps 对象
时,我看到位字段(gps.clean、gps.data_valid)已转换为 TextFields。我尝试通过简单地将 TextField
更改为 NullBooleanField
来快速修复这些问题。现在,在管理页面上,我看到一个适当的下拉菜单,其中包含选项:未知、是、否。不幸的是,现有的位值 1 被解析为未知。
我不知道如何解决这个问题,尝试更改数据库结构会出现问题。
<小时/>4.再次在管理中,我无法正确加载 gps_zda 对象。起初,我收到一个错误,提示 gps_zda.id 列不存在。我能够通过改变来解决这个问题
host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')
...
gps = models.ForeignKey(Gps)
至
host = models.ForeignKey(Gps, primary_key=True, db_column='host', related_name='gps_zda_host_fk')
...
gps = models.ForeignKey(Gps, primary_key=True)
这让 Django 知道不要寻找 gps_zda.id 字段。
我还在 GpsZda 的 Meta 类中添加了 `unique_together = (('gps', 'host'),)。
但是,我仍然无法通过管理页面查看 gps_zda 数据。我收到以下错误 主键 u'Arthur' 的 gps zda 对象不存在。
其中 Arthur
是有效的 gps.host 值。
当我开始研究 Django 时,我认为它会很有趣。没那么多。我真的很怀疑将任何现有数据库转移到 Django 项目中是否可行。
这是我当前的模型。任何关于如何让它们更好地与我的 table 配合使用的建议都将受到欢迎。
class Gps(models.Model):
longitude_dir = models.CharField(max_length=1, blank=True, null=True)
longitude = models.DecimalField(max_digits=8, decimal_places=5, blank=True, null=True)
latitude = models.DecimalField(max_digits=7, decimal_places=5, blank=True, null=True)
id = models.AutoField(primary_key=True)
host_time = models.DateTimeField(primary_key=True)
latitude_dir = models.CharField(max_length=1, blank=True, null=True)
host = models.CharField(primary_key=True, max_length=24)
altitude_units = models.CharField(max_length=1, blank=True, null=True)
gps_time = models.DateTimeField(blank=True, null=True)
raw = models.TextField()
clean = models.NullBooleanField(null=True) # This field type is a guess.
data_valid = models.NullBooleanField(null=True) # This field type is a guess.
modem = models.CharField(max_length=15)
altitude = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
class Meta:
managed = False
db_table = 'gps'
unique_together = (('id', 'host'),)
class GpsZda(models.Model):
local_zone_minutes = models.IntegerField(blank=True, null=True)
host = models.ForeignKey(Gps, db_column='host', related_name='gps_zda_host_fk')
year = models.IntegerField(blank=True, null=True)
day = models.IntegerField(blank=True, null=True)
month = models.IntegerField(blank=True, null=True)
local_zone = models.IntegerField(blank=True, null=True)
gps = models.ForeignKey(Gps, primary_key=True, unique=True)
timestamp = models.TimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'gps_zda'
unique_together = (('gps', 'host'),)
最佳答案
Django 目前不支持表中的复合主键。 (参见 wiki page 和缺少 model meta options 这是定义它的地方。)
最好的选择可能是向您的 gps
表中添加另一个标识列,以便 Django 可以正常使用它。
对于其他问题,最好研究并针对它们发布单独的问题。
关于python - Django - 旧版 MySQL 数据迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30039002/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!