Giter Club home page Giter Club logo

django-custom-indexes's Introduction

django-custom-indexes

Django PostgreSQL backend and custom fields to create custom indexes.

With this, indexes can be defined for a model using field attributes which will be migrated to the db, this eliminates the need to write migrations manually with RunSQL to create indexes.

Installation

The package can be installed by running the following command:

pip install django-custom-indexes

PyPI: https://pypi.python.org/pypi/django-custom-indexes/

Add django_custom_indexes to your INSTALLED_APPS:

INSTALLED_APPS = (
    '...',
    'django_custom_indexes'
)

Requirements:

  • Django 1.7 to 1.11 (not tested for 2.0+).
  • PostgreSQL database backend.
  • Python 2.7

Usage

Creating Indexes in PostgresSQL: https://www.postgresql.org/docs/9.5/static/sql-createindex.html

In your settings update the ENGINE parameter of DATABASES to 'django_custom_indexes.backends.postgresql_psycopg2'

By default custom_indexes backend inherits from django.db.backends.postgresql_psycopg2, to use some other sub class of postgresql_psycopg2 add the following setting:

DJANGO_CUSTOM_INDEXES_BASE_ENGINE

Example Usage:

from django_custom_indexes import model_fields
from django.db import models


class MyModel(models.Model):
    my_field1 = model_fields.CustomCharField(
        max_length=100,
        custom_indexes=[
            {
                'unique': True, # Optional, used to create unique indexes
                'name': 'custom_unique_index', # Optional (auto generated), Required only if no columns are specified
                'where': 'my_field2 > 0', # Optional, used to create partial Indexes
            },
            {
                'unique': True,
                'columns': ['lower(my_field1)'], # Optional, Specify columns or expressions for the index
            }
        ]
    )
    my_field2 = model_fields.CustomIntegerField(
        custom_indexes=[
            {
                'name': 'custom_gin_index1',
                'using': 'USING gin (my_field2)', # Optional, Specify which method to use for the index
            }
        ]
    )

django-custom-indexes's People

Contributors

anshul-chhangani avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sztamas

django-custom-indexes's Issues

WHERE condition not added automatically to CREATE_INDEX

From the README.md Example Usage:

        custom_indexes=[
            {
                'unique': True, # Optional, used to create unique indexes
                'name': 'custom_unique_index', # Optional (auto generated), Required only if no columns are specified
                'where': 'my_field2 > 0', # Optional, used to create partial Indexes
            },

it looks like you expect users to specify just the condition without the WHERE.

Doing that though you'll end up with a syntax error, because there is no WHERE in the sql_create_custom_index template https://github.com/joshtechnologygroup/django-custom-indexes/blob/master/django_custom_indexes/backends/postgresql_psycopg2/schema.py#L20

columns isn't optional like the documentation says

From the README.md Example Usage:

        custom_indexes=[
            ...
            {
                'unique': True,
                'columns': ['lower(my_field1)'], # Optional, Specify columns or expressions for the index
            },

However, columns isn't optional. If you don't specify it, you will get a syntax error because the field you declared the custom index on doesn't get added automatically.

Support for Django 1.11+

Is there a reason not to support Django 1.11+?

The docs say that you can create indexes in Django using Meta options starting with 1.11, but I don't think Django supports creating partial unique indexes and that's what I'm using your package for.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.