2009-10-02 3 views
1

보시다시피 create()는 작동하지만 get_or_create()는 작동하지 않습니다. 내가 여기서 뭔가 분명한 것을 놓치고 있니?일반 외래 키 및 django 1.0 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 

답변

2

봐가 다른 get_or_create에로 createget_or_create에 사용되는 논리를 더 source 인수가 없지만 src_object_idsrc_content_type이야 있지만이 문제를 해결하기 쉽습니다 같은 - (이명 령과 동일) u.content_typeu.idscr_object_idsrc_content_type 통과 .

try/except & create을 사용하십시오.

+0

try/except와 create를 사용하여 직접 get_or_create를 다시 구현하는 데주의하십시오. 실시간 동시로드에서만 오류가 발생하는 경쟁 조건을 만드는 것은 매우 쉽습니다. get_or_create 소스를 조사하여 문제 해결 방법을 확인하십시오. –