jleivaizq / djorm-ext-pgtrgm Goto Github PK
View Code? Open in Web Editor NEWDjango application with some addons regarding PostgreSQL trigram text comparison
License: GNU General Public License v3.0
Django application with some addons regarding PostgreSQL trigram text comparison
License: GNU General Public License v3.0
I tried the pip version of this plugin as found here https://pypi.python.org/pypi/djorm-ext-pgtrgm/0.3 & got the following error.
File "venv/lib/python3.5/site-packages/djorm_pgtrgm/init.py", line 1, in
from django.db import backend
ImportError: cannot import name 'backend'
The same was mitigated when I downloaded the source from GitHub. Please update the pip package
I am using Django 1.9.4
>>> from djorm_pgtrgm import SimilarManager
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: cannot import name SimilarManager
>>>
>>> dir(djorm_pgtrgm)
['ALL_TERMS', 'Field', 'NEW_LOOKUP_TYPE', 'QUERY_TERMS', '__builtins__', '__doc__', '__file__', '__name__', '__package_
_', '__path__', 'backend', 'backend_allowed', 'connection', 'db_backends_allowed', 'get_db_prep_lookup', 'get_prep_look
up', 'monkey_get_db_prep_lookup', 'subclassing']
>>>
Let's say you do something like:
Person.objects.filter_o(name__similar='Bobby').select_related('Employer')
if the Employer model also has a name field, you'll get an exception:
Exception Value: column reference "name" is ambiguous
LINE 1: SELECT (similarity(name, 'Bobby')) AS "name_distance...
The SimilarQuerySet class does not include the table name when specifying the field to filter on. To solve this problem you need something like this:
def filter_o(self, **kwargs):
qs = super(SimilarQuerySet, self).filter(**kwargs)
for lookup, query in kwargs.items():
if lookup.endswith('__similar'):
field = lookup.replace('__similar', '')
select = {'%s_distance' % field: "similarity(%s.%s, '%s')" % (self.model._meta.db_table, field, query)}
qs = qs.extra(select=select).order_by('-%s_distance' % field)
return qs
(notice the addition of self.model._meta.db_table
).
This line makes djorm-ext-pgtgrm
depend on postgis.
That, of course, is not necessary.
When a django ORM .filter()
precedes a pgtrgm .filter_o()
it raises an AttributeError because the parent model manager returns a QuerySet object rather than a SimilarQuerySet object. SimilarManager should override filter in order to allow it to work with filters that precede or follow a filter_o()
. I'll submit a PR.
Django 1.6, postgres 9.1.13 with CREATE EXTENSION pg_trgm;
>>> FooVault.objects.filter(subject__icontains="still")
[<FooVault: 3805>]
>>> FooVault.objects.filter(subject__similar="still")
[]
>>>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.