postgres.search.SearchRank

class SearchRank(vector, query, weights=None) [source] So far, we’ve just returned the results for which any match between the vector and the query are possible. It’s likely you may wish to order the results by some sort of relevancy. PostgreSQL provides a ranking function which takes into account how often the query terms appear in the document, how close together the terms are in the document, and how important the part of the document is where they occur. The better the match, the higher

postgres.search.SearchVector

class SearchVector(*expressions, config=None, weight=None) [source] Searching against a single field is great but rather limiting. The Entry instances we’re searching belong to a Blog, which has a tagline field. To query against both fields, use a SearchVector: >>> from django.contrib.postgres.search import SearchVector >>> Entry.objects.annotate( ... search=SearchVector('body_text', 'blog__tagline'), ... ).filter(search='Cheese') [<Entry: Cheese on Toast recipes>

postgres.search.SearchVectorField

class SearchVectorField [source] If this approach becomes too slow, you can add a SearchVectorField to your model. You’ll need to keep it populated with triggers, for example, as described in the PostgreSQL documentation. You can then query the field as if it were an annotated SearchVector: >>> Entry.objects.update(search_vector=SearchVector('body_text')) >>> Entry.objects.filter(search_vector='cheese') [<Entry: Cheese on Toast recipes>, <Entry: Pizza recipes>]

postgres.search.TrigramDistance

class TrigramDistance(expression, string, **extra) [source] New in Django 1.10. Accepts a field name or expression, and a string or expression. Returns the trigram distance between the two arguments. Usage example: >>> from django.contrib.postgres.search import TrigramDistance >>> Author.objects.create(name='Katy Stevens') >>> Author.objects.create(name='Stephen Keats') >>> test = 'Katie Stephens' >>> Author.objects.annotate( ... distance=Tr

postgres.search.TrigramSimilarity

class TrigramSimilarity(expression, string, **extra) [source] New in Django 1.10. Accepts a field name or expression, and a string or expression. Returns the trigram similarity between the two arguments. Usage example: >>> from django.contrib.postgres.search import TrigramSimilarity >>> Author.objects.create(name='Katy Stevens') >>> Author.objects.create(name='Stephen Keats') >>> test = 'Katie Stephens' >>> Author.objects.annotate( ... simil

postgres.validators.KeysValidator

class KeysValidator(keys, strict=False, messages=None) [source] Validates that the given keys are contained in the value. If strict is True, then it also checks that there are no other keys present. The messages passed should be a dict containing the keys missing_keys and/or extra_keys. Note Note that this checks only for the existence of a given key, not that the value of a key is non-empty.

postgres.validators.RangeMaxValueValidator

class RangeMaxValueValidator(limit_value, message=None) [source] Validates that the upper bound of the range is not greater than limit_value.

postgres.validators.RangeMinValueValidator

class RangeMinValueValidator(limit_value, message=None) [source] Validates that the lower bound of the range is not less than the limit_value.

PostgreSQL specific lookups

Trigram similarity New in Django 1.10. The trigram_similar lookup allows you to perform trigram lookups, measuring the number of trigrams (three consecutive characters) shared, using a dedicated PostgreSQL extension. A trigram lookup is given an expression and returns results that have a similarity measurement greater than the current similarity threshold. To use it, add 'django.contrib.postgres' in your INSTALLED_APPS and activate the pg_trgm extension on PostgreSQL. You can install the exte

Providing initial data for models

It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. You can provide initial data via fixtures. Providing initial data with fixtures A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML install