Search code examples
pythondjangoelasticsearchdjango-haystack

Haystack SearchFieldError : The model 'None' combined with model_attr <attr_name> returned None


On running rebuild_index, objects from other app are getting indexed but on one app SearchFieldError is happening.

This is my models.py for the concerned app :

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"),
        )

And my search_indexes.py is :

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()

These are the versions:

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

What am I doing wrong here. Please help!!


Solution

  • Try Adding default=' ' in your models for attributes you are getting error Or Try Adding null=True for attributes you are getting error in DoctorIndex It worked for me a while ago For example

    example_field = indexes.CharField(model_attr='example_field', null=True)