Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通用外键并在django 1.0中获取或创建:断开?_Python_Django_Foreign Keys - Fatal编程技术网

Python 通用外键并在django 1.0中获取或创建:断开?

Python 通用外键并在django 1.0中获取或创建:断开?,python,django,foreign-keys,Python,Django,Foreign Keys,如您所见,create()可以工作,但get\u或\u 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") -----------------------------------------

如您所见,create()可以工作,但get\u或\u 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
[7]中的
:f=FeedItem.objects.create(source=u,dest=q,type=“greata”)
在[8]:f中,created=FeedItem.objects.get\u或\u create(source=u,dest=q,type=“greata”)
---------------------------------------------------------------------------
FieldError回溯(最近一次呼叫上次)
/用户/andrew/小丑鱼/panda repo/community feed/in()
/get_或_create(self,**kwargs)中的Library/Python/2.6/site-packages/django/db/models/manager.pyc
/get_或_create(self,**kwargs)中的Library/Python/2.6/site-packages/django/db/models/query.pyc
/get中的Library/Python/2.6/site-packages/django/db/models/query.pyc(self,*args,**kwargs)
/过滤器中的Library/Python/2.6/site-packages/django/db/models/query.pyc(self,*args,**kwargs)
/Library/Python/2.6/site-packages/django/db/models/query.pyc in_filter_或_exclude(self、negate、*args、**kwargs)
/add_q(self,q_对象,used_别名)中的Library/Python/2.6/site-packages/django/db/models/sql/query.pyc
/添加过滤器中的Library/Python/2.6/site-packages/django/db/models/sql/query.pyc(self、filter\expr、connector、negate、trim、can\u reuse、process\u extras)
/设置联接中的Library/Python/2.6/site-packages/django/db/models/sql/query.pyc(self、names、opts、alias、dupe\u multis、allow\u many、allow\u explicit\u fk、can\u重用、否定、处理附加)
FieldError:无法将关键字“source”解析为字段。选项有:dest\u content\u type、dest\u object\u id、id、src\u content\u type、src\u object\u id、时间戳、类型、权重

看起来在
create
get\u或\u create
中使用了不同的逻辑,就像在
get\u或\u create
中一样,没有
source
参数,但是
src\u object\u id
src\u content\u type
但是很容易解决这个问题-pass
scr\u object\u id
作为
u.id
src\u content\u type
u.content\u type
(与dest相同)


或者使用
try/except
create

小心使用try/except和create重新实现您自己的get\u或create。很容易创建一个竞争条件,它只会在实时并发负载下导致错误。检查get_或_create源代码,查看它如何处理该问题。