如您所见,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
答案 0 :(得分:2)
看起来create
和get_or_create
中使用了不同的逻辑,因为get_or_create
中没有source
参数,但是src_object_id
和src_content_type
但是这很容易解决 - 将scr_object_id
作为u.id
传递给src_content_type
作为u.content_type
(与dest相同)。
或使用try/except
&amp; create
。