django 1.0中的通用外键和get_or_create:破碎?

时间:2009-10-02 22:42:05

标签: 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

1 个答案:

答案 0 :(得分:2)

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

或使用try/except&amp; create