Skip to content

Commit

Permalink
Fixed exception message when resolving model_attr
Browse files Browse the repository at this point in the history
This fixes the error message displayed when model_attr references an
unknown attribute.

Thanks to @wicol for the patch

Closes #1094
  • Loading branch information
wicol authored and acdha committed Jan 21, 2015
1 parent 4dd7d99 commit 59fa15f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ Thanks to
* @Xaroth for updating the app loader to be compatible with Django 1.7
* Jaroslav Gorjatsev (jarig) for a bugfix with index_fieldname
* Dirk Eschler (@deschler) for app loader Django 1.7 compatibility fixes
* Wictor (wicol) for a patch improving the error message given when model_attr references a non-existent
field
8 changes: 4 additions & 4 deletions haystack/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ def prepare(self, obj):

for attr in attrs:
if not hasattr(current_object, attr):
raise SearchFieldError("The model '%s' does not have a model_attr '%s'." % (repr(obj), attr))
raise SearchFieldError("The model '%s' does not have a model_attr '%s'." % (repr(current_object), attr))

current_object = getattr(current_object, attr, None)

if current_object is None:
if self.has_default():
current_object = self._default
# Fall out of the loop, given any further attempts at
# accesses will fail misreably.
# accesses will fail miserably.
break
elif self.null:
current_object = None
# Fall out of the loop, given any further attempts at
# accesses will fail misreably.
# accesses will fail miserably.
break
else:
raise SearchFieldError("The model '%s' has an empty model_attr '%s' and doesn't allow a default or null value." % (repr(obj), attr))
raise SearchFieldError("The model '%s' combined with model_attr '%s' returned None, but doesn't allow a default or null value." % (repr(obj), self.model_attr))

if callable(current_object):
return current_object()
Expand Down

0 comments on commit 59fa15f

Please sign in to comment.