python - django 1.0 中的通用外键和 get_or_create : Broken?

标签 python django foreign-keys

如您所见,create() 有效,但 get_or_create() 无效。我在这里遗漏了一些明显的东西吗?

In [7]: f = FeedItem.objects.create(source=u, dest=q, type="greata")

In [8]: f, created = FeedItem.objects.get_or_create(source=u, dest=q, type="greata")
---------------------------------------------------------------------------
FieldError                                Traceback (most recent call last)

/Users/andrew/clownfish/panda-repo/community-feed/<ipython console> in <module>()

/Library/Python/2.6/site-packages/django/db/models/manager.pyc in get_or_create(self, **kwargs)

/Library/Python/2.6/site-packages/django/db/models/query.pyc in get_or_create(self, **kwargs)

/Library/Python/2.6/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)

/Library/Python/2.6/site-packages/django/db/models/query.pyc in filter(self, *args, **kwargs)

/Library/Python/2.6/site-packages/django/db/models/query.pyc in _filter_or_exclude(self, negate, *args, **kwargs)

/Library/Python/2.6/site-packages/django/db/models/sql/query.pyc in add_q(self, q_object, used_aliases)

/Library/Python/2.6/site-packages/django/db/models/sql/query.pyc in add_filter(self, filter_expr, connector, negate, trim, can_reuse, process_extras)

/Library/Python/2.6/site-packages/django/db/models/sql/query.pyc in setup_joins(self, names, opts, alias, dupe_multis, allow_many, allow_explicit_fk, can_reuse, negate, process_extras)

FieldError: Cannot resolve keyword 'source' into field. Choices are: dest_content_type, dest_object_id, id, src_content_type, src_object_id, timestamp, type, weight

最佳答案

看起来 createget_or_create 中使用了不同的逻辑,因为在 get_or_create 中没有 source 参数,但是src_object_idsrc_content_type 但解决这个问题很容易 - 将 scr_object_id 传递为 u.idsrc_content_type as u.content_type(与 dest 相同)。

或者使用try/except & create

关于python - django 1.0 中的通用外键和 get_or_create : Broken?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1512152/

相关文章:

mysql - 在父表中更新后更新子表中多行的父键的正确方法

python - 如何从 Python 访问 Oracle?

python - Django的reverse_lazy配置不正确

mysql - 具有不为空的外键引用可为空的列

python - 奇怪的 Django AuthenticationForm 行为

python - 这个日期检查是否更简洁?

MySQL外键约束、级联删除

python - 在 python 中将前导零添加到十六进制值

python - 在生成器上使用枚举来解析文本

python - 在 matplotlib 动画模块中管理动态绘图