daevaorn / djapian

High level Xapian integration for Django
Other
6 stars 3 forks source link

Indexer fails with null values #117

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The indexer fails at processing null values in database fields:

8<------------------------------------------------------------------------------
---------
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/
django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/
django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/
django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/
django/core/management/base.py", line 218, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/Djapian-2.3.1-py2.6.egg/
djapian/management/commands/index.py", line 166, in handle
    update_changes(verbose, timeout, not make_daemon, per_page,
commit_each)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/
django/db/transaction.py", line 338, in _commit_manually
    return func(*args, **kw)
  File "/usr/local/lib/python2.6/dist-packages/Djapian-2.3.1-py2.6.egg/
djapian/management/commands/index.py", line 75, in update_changes
    commit_each
  File "/usr/local/lib/python2.6/dist-packages/Djapian-2.3.1-py2.6.egg/
djapian/indexer.py", line 233, in update
    index_value = field.convert(value, self._model)
  File "/usr/local/lib/python2.6/dist-packages/Djapian-2.3.1-py2.6.egg/
djapian/indexer.py", line 44, in convert
    value = '%012d' % field_value
TypeError: %d format: a number is required, not NoneType
8<------------------------------------------------------------------------------
---------

The affected field is marked with "null=True" in the django model.  I
solved this by adding the following line to the convert method of the
Field class (file indexer.py line 34):

8<------------------------------------------------------------------------------
---------
def convert(self, field_value, model):
    """
    Generates index values (for sorting) for given field value and its
content type
    """
    if field_value is None:
        return None
    # If it is a model field make some postprocessing of its value
    [...]
8<------------------------------------------------------------------------------
---------

I'm using djapian 2.3.1 with django 1.2.1

Original issue reported on code.google.com by apoi...@gmail.com on 14 Jul 2010 at 1:21

GoogleCodeExporter commented 9 years ago
I get a similar error when running ./manage.py index --rebuild, though mine is 
on a DateTimeField:

----------------------------------------
Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_manager(settings)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 218, in execute
    output = self.handle(*args, **options)
  File "/home/user/djapian/management/commands/index.py", line 164, in handle
    rebuild(verbose, per_page, commit_each)
  File "/home/user/djapian/management/commands/index.py", line 129, in rebuild
    indexer.update(None, after_index, per_page, commit_each)
  File "/home/user/djapian/indexer.py", line 232, in update
    index_value = field.convert(value, self._model)
  File "/home/user/djapian/indexer.py", line 53, in convert
    value = field_value.strftime('%Y%m%d%H%M%S')
AttributeError: 'NoneType' object has no attribute 'strftime'
----------------------------------------

apoisel's patch seems to work for me.

Original comment by spamcome...@gmail.com on 16 Jul 2010 at 8:31

GoogleCodeExporter commented 9 years ago
I just updated to the current trunk (r366) and I see that this bug has already 
been fixed.  Thank you!

Original comment by spamcome...@gmail.com on 16 Jul 2010 at 9:37

GoogleCodeExporter commented 9 years ago
Fixed

Original comment by daevaorn on 19 Jul 2010 at 4:42