etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Raise TypeError or such when specifying non-iterable argument for 'fields' when registering model #126

Closed j1fig closed 7 years ago

j1fig commented 9 years ago

This is more of user-friendliness issue:

    watson.register(Community, fields=('name'))

This causes field attribute in registration.SearchAdapter to have self.fields = 'name', which will then be iterated here, which causes it to try to access models field, 'n', 'a', 'm', 'e' - which dont exist.

This is of course because ('name') is the same as 'name', and could be boiled down to user ingenuity (such as mine).

Doing

    watson.register(Community, fields=('name',))

Fixes the problem, as SearchAdapter.fields will now be an iterable.

Thought it was worth posting here, for future newbie reference (though it concerns Python syntax, I wouldn't consider it an issue). Raising a TypeError when specifying a non-iterable object as arg to fields in watson.register might make it easier though.

Up to @etianen ;)

Best, Joao

etianen commented 9 years ago

I really hate that piece of Python syntax!

How many times do we have to write:

if isinstance(value, abc.Iterator) and not isinstance(value, (str, bytes)):

But you have a good ergonomic point. I'll have a look next week. On Tue, 22 Sep 2015 at 10:55, João Figueiredo notifications@github.com wrote:

This is more of user-friendliness issue, and might :

watson.register(Community, fields=('name'))

Causes field attribute in registration.SearchAdapter to have self.fields = 'name', which will then be iterated here https://github.com/etianen/django-watson/blob/master/src/watson/registration.py#L138, which causes it to try to access models field, 'n', 'a', 'm', 'e' - which dont exist.

This is of course because ('name') is the same as 'name', and could be boiled down to user ingenuity (such as mine).

Doing

watson.register(Community, fields=('name',))

Fixes the problem, as SearchAdapter.fields will now be an iterable.

Thought it was worth posting here, for future newbie reference (though it concerns Python syntax, I wouldn't consider it an issue). Raising a TypeError when specifying a non-iterable object as arg to fields in watson,register might make it easier though.

Up to @etianen https://github.com/etianen ;)

Best, Joao

— Reply to this email directly or view it on GitHub https://github.com/etianen/django-watson/issues/126.