Giter Club home page Giter Club logo

djorm-ext-pgtrgm's Issues

Update pip package

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

djorm-ext-pgtrgm==0.1 from pip missing SimilarManager()

>>> 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']
>>>

SimilarQuerySet specifies the field name in an ambiguous manner

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).

AttributeError: 'QuerySet' object has no attribute 'filter_o'

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.

__similar not finding matches

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")
[]
>>> 

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.