django-haystack / django-haystack/django-haystack

Subclassing fields for value cleanup does not work in haystack2 any more

Open
#611 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs review
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.