Haystack SearchFieldError:模型&#39;无&#39;与model_attr <attr_name>结合使用返回None

时间:2017-01-20 10:42:46

标签: python django elasticsearch django-haystack

在运行rebuild_index时,来自其他应用程序的对象正在编入索引但在一个应用程序上正在发生SearchFieldError。

这是我对相关应用的models.py:

class Doctor(models.Model):
    SPECIALTY_CHOICES = (
        ('Pediatric Physiotherapist', 'Pediatric Physiotherapist')
    )
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    doctorName = models.CharField(max_length=100)
    doctorQual = models.CharField(max_length=500)
    doctorSpecial = models.CharField(choices=SPECIALTY_CHOICES, default='', max_length=50)
    doctorSummary = models.CharField(max_length=1000)
    doctorLocation = models.CharField(max_length=100, default=' ', null=True, blank=True)
    slug = models.SlugField(null=True, blank=True)
    city = models.CharField(max_length = 100, null=True, blank=True)

    def __str__(self):
        return self.doctorName

    #   save method overridden to generate slug for each object

    def save(self, *args, **kwargs):
        self.slug = slugify(self.doctorName)
        super(Doctor, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return "/doctor/doc_detail_page/%i" % self.id

    def get_summary(self):
        return self.doctorSummary

    class Meta:
        permissions = (
            ("can_create", "yoyo"),
        )

我的search_indexes.py是:

from haystack import indexes
from .models import Doctor


class DoctorIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    doctorName = indexes.CharField(model_attr='doctorName')
    doctorSummary = indexes.CharField(model_attr='doctorSummary')
    doctorSpecial = indexes.CharField(model_attr='doctorSpecial', faceted=True)
    doctorLocation = indexes.CharField(model_attr='doctorLocation',faceted=True)
    content_auto_doc = indexes.EdgeNgramField(model_attr='doctorName')     # this field is for auto complete

    def get_model(self):
        return Doctor

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

这些是版本:

  • elasticsearch == 5.0.1 django-haystack == 2.5.1 django - 1.10

我在这里做错了什么。请帮忙!!

1 个答案:

答案 0 :(得分:3)

尝试在模型中添加default =''以获取您收到错误的属性 要么 对于DoctorIndex中出现错误的属性,请尝试添加null = True 它不久前对我有用了 例如

if(event.getAction() == MotionEvent.ACTION_UP)