Django& haystack:ManyToMany表单字段无法保存

时间:2014-07-20 12:42:54

标签: django django-forms django-haystack

我有这个由Haystack索引的模型:

class Proposal(models.Model):
    id = models.AutoField(primary_key=True)
    text = models.TextField()
    proposer = models.ForeignKey("User", related_name="proposed")
    tags = models.ManyToManyField(Tag, related_name="proposals")

索引:

{{ object.title }}
{{ object.text }}
{% for tag in object.tags.all %}{{ tag.name }} {% endfor %}

这是在提交表单时视图中发生的情况:

if form.is_valid():
            proposal = form.save(commit=False)
            proposal.proposer = user
            proposal.save()
            return HttpResponseRedirect('/proposal/'+str(proposal.id))

Django docs告诉我,在倒数第二行调用了proposal.save()之后,我需要调用proposal.save_m2m()。问题是调用proposal.save()给了我一个AssertionError“没有提供异常消息”。看看traceback,我可以看到问题是由Haystack造成的,或者更具体地说是我使用的搜索库,Whoosh。

Traceback:
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/tao/dev/myproject/myapp/views.py" in submit
  136.             proposal.save()
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/django/db/models/base.py" in save
  545.                        force_update=force_update, update_fields=update_fields)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/django/db/models/base.py" in save_base
  582.                                    update_fields=update_fields, raw=raw, using=using)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/django/dispatch/dispatcher.py" in send
  185.             response = receiver(signal=self, sender=sender, **named)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/haystack/signals.py" in handle_save
  48.                 index.update_object(instance, using=using)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/haystack/indexes.py" in update_object
  274.                 backend.update(self, [instance])
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/haystack/backends/whoosh_backend.py" in update
  208.             writer.commit()
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in commit
  1040.             self.writer.commit(*args, **kwargs)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in commit
  925.         finalsegments = self._merge_segments(mergetype, optimize, merge)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in _merge_segments
  830.         return mergetype(self, self.segments)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in MERGE_SMALL
  88.                 writer.add_reader(reader)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in add_reader
  710.         self.add_postings_to_pool(reader, basedoc, docmap)
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in add_postings_to_pool
  634.         for item in items:
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/writing.py" in _process_posts
  570.         for fieldname, text, docnum, weight, vbytes in items:
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/reading.py" in iter_postings
  432.                 yield (fieldname, btext, m.id(), m.weight(), m.value())
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/codec/whoosh3.py" in value
  928.             self._read_values()
File "/Users/tao/.virtualenvs/mydjangoproject/lib/python3.4/site-packages/whoosh/codec/whoosh3.py" in _read_values
  1049.             assert isinstance(vs, bytes_type)

Exception Type: AssertionError at /submit/
Exception Value:

在保存多对多字段时,我无法在网上找到任何有关Haystack的信息。我的代码是否有一个明显的问题,我错过了?

0 个答案:

没有答案