Django: Using the ORM to (automatically) create custom database field types

When creating the full text index search layer in Django, I had a little issue when new models were created.

Luckily, it's quite easy to create custom database types in your models.

class TSearchVectorField(models.fields.TextField):
This lets Django know how to create a field with the "tsvector" type.
description = "Tsearch Vector"

def db_type(self, connection):
return 'tsvector'

class TSearch(models.Model):
fti = TSearchVectorField()

Elegant, like a flock of Seagals.


Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog