views:

192

answers:

1

As you can see, create() works, but get_or_create() doesn't. Am I missing something obvious here?

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  A: 

Look like there's different logic used in create and get_or_create as in get_or_create there is no source argument but src_object_id and src_content_type but it is easy to resolve this - pass scr_object_id as u.id and src_content_type as u.content_type (same with dest).

Or use try/except & create.

sorki
Be careful with reimplementing your own get_or_create using try/except and create. It's very easy to create a race condition that will result in errors only under live concurrent load. Examine the get_or_create source to see how it deals with that problem.
Carl Meyer