Django dump:模型未验证
当我尝试使用此指令在 Django 中转储模型的数据时:
python manage.py dumpdata app> temp_data.json
它给出了以下错误:
Error: One or more models did not validate:
asset.authpermission: "codename": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype: "app_label": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype: "model": CharField cannot have a "max_length" greater than 255 when using "unique=True".
问题是这些表是由 django 自动生成的。另外,我刚刚检查了数据库(mysql),字段是varchar(100)。
怎么了 ?
When I try to dump the data of my model in Django with this instruction :
python manage.py dumpdata app> temp_data.json
It gives the following error :
Error: One or more models did not validate:
asset.authpermission: "codename": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype: "app_label": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype: "model": CharField cannot have a "max_length" greater than 255 when using "unique=True".
The thing is that these tables are auto-generated by django. Plus I just checked in the database (mysql) and the fields are varchar(100).
What's wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些表是由
manage.pyspectdb
生成的,对吧?那么你不需要将django自己的模型包含到实际生成的模型中。只需删除以auth
、django
、admin
和site
开头的任何模型即可。只需将相应的贡献应用程序包含到 INSTALLED_APPS 设置中即可,瞧,不再有错误。
Those tables are generated by a
manage.py inspectdb
right? Then you don't need to include django's own models into the actual generated models. Just remove any model starting withauth
,django
,admin
andsite
.Just include the corresponding contrib apps to the INSTALLED_APPS setting and voila, no more errors.
Django 正在尝试根据数据库后端允许的模式验证您的架构,然后再允许您转储。问题是这样的:
您遇到的问题是这样的:
CharField
在 SQL 中转换为VARCHAR
- 如果您有max_length
转换为VARCHAR(X )
其中X
是您设置的最大长度。对于非 MyISAM 的表,MySQL 不会为搜索
CharField
中任何长度超过255
个字符的数据建立索引(散列)。这也排除了TextField
转换为 SQLTEXT
的情况。关于索引的 MySQL 文档相当全面。
CREATE INDEX
文档让您了解问题的核心:换句话说,除非您使用 MyISAM 作为存储格式,否则无法执行此操作。
无论哪种方式,对于这是否是设计缺陷都存在争议。一方面,您可能会说,实际上,如果您需要将如此巨大的数据桶索引为一堆文本,那么您确实需要更仔细地考虑这一点。另一方面,您可能会认为 255 是一个任意的限制选择。为什么是 255 而不是 300?还是200?我
自己刚刚发现了这个问题。解决方案是询问
unique=True
是否确实需要,然后确定max_length
是否确实需要那么长。就我而言,我们存储的是 sha512 输出,长度为 128 个字符,因此我只是适当调整了字段的大小。但是,如果您确实需要唯一的长数据,您可以通过重写模型
save()
方法自行强制执行此操作。您真正想要做的是覆盖full_clean
方法,该方法由django.forms.*
调用,以根据架构验证模型的数据,然后覆盖一个save
方法调用它。这样,无论您直接保存还是在表单上调用is_valid()
,都会出现唯一性约束。另一种选择是切换到 MyISAM,或使用 postgres。
Django is attempting to validate your schema against what the database backend will allow before it lets you dump. The issue is this:
The problem you're experiencing is this:
CharField
translates toVARCHAR
in SQL - if you havemax_length
that translates toVARCHAR(X)
whereX
is whatever you set the maximum length to.MySQL, for tables that aren't MyISAM, won't index (hash) for searching any data longer than
255
characters in aCharField
. This also rules outTextField
which translates to SQLTEXT
.The MySQL docs on indexing are fairly comprehensive. The
CREATE INDEX
documentation gets you to the heart of the issue:In other words, unless you're using MyISAM as your storage format, you can't do this.
There is an argument either way as to whether this is a design fault or not; on the one hand, you could argue that really, if you need to index such a huge bucket of data as a full pile of text, you really need to think about that more carefully. On the other hand, you could argue that 255 is an arbitrary choice for a limit. Why 255 and not 300? Or 200? I d
I've just hit upon this issue myself. The solution is to ask whether
unique=True
is actually required, then decide ifmax_length
really does need to be that long. In my case we were storing sha512 output, which is 128 chars long, so I just resized the fields appropriately.However, if you do need unique long data, you can enforce this yourself by overriding a models
save()
method. What you actually want to do is override thefull_clean
method, which is called bydjango.forms.*
to validate your model's data against the schema, then have an overriddensave
method call that. That way, the uniqueness constraint occurs whether you save directly or callis_valid()
on a form.The other option is to switch to MyISAM, or use postgres.