django-haystack / django-haystack/django-haystack
Subclassing fields for value cleanup does not work in haystack2 any more
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 3.7k
- Forks
- 1.3k
- Avg merge
- 3h 18m
- Merged PRs (30d)
- 3
Description
In haystack 1.2.X, I used something along the lines of
class HtmlField(indexes.CharField):
def prepare_template(self, instance):
t = super(HtmlField, self).prepare_template(instance)
t = BeautifulSoup(t, convertEntities=BeautifulSoup.HTML_ENTITIES)
return t
to clean up certain fields before handing them to the backend. However, this does not work any more in the master branch. Instead, I had to resort to overriding index.full_prepare which doesn't feel right.
I think field.prepare(obj) around index.py line 200 should also be called in the "have prepare_foo" case to allow for such customisation, ie. the code should look like this:
if hasattr(self, "prepare_%s" % field_name):
value = getattr(self, "prepare_%s" % field_name)(obj)
self.prepared_data[field.index_fieldname] = field.prepare(value)
else:
self.prepared_data[field.index_fieldname] = field.prepare(obj)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in index.py around line 200 and trace the have-prepare_foo branch through _field.prepare(value) and full_prepare. Confirm that custom field subclasses can clean values before they reach the backend, then verify the behavior with the relevant indexing path and its existing checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, search
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100