在 Django 装置中指定 ManyToMany 字段

发布于 2024-12-17 09:46:30 字数 2311 浏览 0 评论 0原文

固定装置的文档说您可以使用主键的 int 值指定自然键。例如:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": 42
}
}

但是它没有说明如何引用manytomany。我尝试过使用数组:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": [42,43,44]
}
}

我编写脚本来生成固定装置的方式,有时数组中只有一个主键:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": [42]
}
}

运行 manage.py loaddata Fixture.json 时,我得到出现以下错误:

Problem installing fixture 'fixtures/NCT00109798.json': Traceback (most recent call last):

File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
obj.save(using=using)

File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)

File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 549, in save_base
result = manager._insert(values, return_id=update_pk, using=using)

File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)

File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 1518, in    insert_query
return query.get_compiler(using=using).execute_sql(return_id)

File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 788, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)

File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
cursor.execute(sql, params)

File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)

File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 175, in execute
if not self._defer_warnings: self._warning_check()

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 89, in _warning_check
warn(w[-1], self.Warning, 3)

Warning: Data truncated for column 'keyword' at row 1

是否有另一种方法可以使用固定装置指定这种关系?

The documentation on fixtures says you can specify a natural key using the int value of the primary key. For example:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": 42
}
}

However it does not say how to reference a manytomany. I have tried using an array:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": [42,43,44]
}
}

The way I've written the script to generate the fixture, sometimes there is only one primary key in the array:

{
"pk": 1,
"model": "store.book",
"fields": {
    "name": "Mostly Harmless",
    "author": [42]
}
}

When running manage.py loaddata fixture.json I get the following error:

Problem installing fixture 'fixtures/NCT00109798.json': Traceback (most recent call last):

File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
obj.save(using=using)

File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)

File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 549, in save_base
result = manager._insert(values, return_id=update_pk, using=using)

File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)

File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 1518, in    insert_query
return query.get_compiler(using=using).execute_sql(return_id)

File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 788, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)

File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
cursor.execute(sql, params)

File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)

File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 175, in execute
if not self._defer_warnings: self._warning_check()

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 89, in _warning_check
warn(w[-1], self.Warning, 3)

Warning: Data truncated for column 'keyword' at row 1

Is there another way to specify this relationship using fixtures?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文