如您所见,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
最佳答案
看起来 create
和 get_or_create
中使用了不同的逻辑,因为在 get_or_create
中没有 source
参数,但是src_object_id
和 src_content_type
但解决这个问题很容易 - 将 scr_object_id
传递为 u.id
和 src_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/