Django Haystack and Elasticsearch- part one

Posted on Sat 06 February 2016

Hello! Today blog post is about Django Haystack and how to integrate it quickly with Elasticsearch.

First after creating django project (At beginning of 2016 django-haystack don't work properly with django 1.9 so I used 1.8.9 version) and making new app let's add models:

from django.db import models

    ('Male', 'Male'),
    ('Female', 'Female')

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
    email = models.CharField(max_length=100)
    ip_address = models.CharField(max_length=100)

    def __str__(self):
        return '{first_name} {last_name}'.format(first_name=self.first_name, last_name=self.last_name)

And register model to the admin site. Don't forget about adding created app to and making makemigrations and migrate after it:

from django.contrib import admin

from .models import Person

Then create simple script wich will load a data from JSON to the database. This JSON data is randomly generated data from this webpage. Call it and place in your django application folder.

# coding=utf-8
import os
import json

from .models import Person

DATA_FILE = os.path.join(

def run(verbose=True):
with open(DATA_FILE) as data_file:
    data = json.load(data_file)
    for record in data:

This script looks for file MOCK_DATA.json. Then based on fields on this JSON loads data to the django application. You can run this by shell and then:

>>> from django_app import load
{'ip_address': '', 'gender': 'Female', 'first_name': 'Christine', 'last_name': 'Cunningham', 'email': '[email protected]'}
{'ip_address': '', 'gender': 'Male', 'first_name': 'Scott', 'last_name': 'Hanson', 'email': '[email protected]'}
# rest of the records

That's all for this week. In next post, I will concentrate on how to setup Elasticsearch and django-haystack.

Cover image by Lets Go Out Bournemouth and Poole under CC BY 2.0.

If you find this blog post interesting please share:

Comments !