Django full text search: TrigramDistance not working - django

I am using Django 2.0 and postgresql 9.6.
I have installed pg_trgm extension in the postgresql
I am trying to understand how triagram works: I am trying the following. I want words nearer to "sridam" to be matched using triagram
from django.contrib.postgres.search import TrigramDistance
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
vector = SearchVector('title',config='english_unaccent', weight='A') + SearchVector('description',config='english_unaccent', weight='B')
query = SearchQuery('sridam',config='english_unaccent')
Article.objects.annotate(distance=TrigramDistance(vector, query)).filter(distance__lte=0.7).order_by('distance')
ProgrammingError: operator does not exist: tsvector <-> tsquery
LINE 1: ...SCE("articles_article"."description", '')), 'B')) <-> plaint...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

You can't calculate the Trigram distance between a SearchVector and a SearchQuery because "A trigram is a group of three consecutive characters.".
As specified in the Django documentation TrigramDistance "accepts a field name or expression, and a string or expression. Returns the Trigram distance between the two arguments.".

Before using postgres extensions you have to apply extension migrations to your model.
SOLUTION 1
Make empty migration file:
python3 manage.py makemigrations --empty yourapp
then go to the new migration file and add following
from django.db import migrations
from django.contrib.postgres.operations import TrigramExtension
class Migration(migrations.Migration):
operations = [
TrigramExtension()
]
Finally run python3 manage.py migrate to apply migrations to your database and you are ready to use the extension.
SOLUTION 2
You can go to psql console and apply migrations directly:
CREATE EXTENSION pg_trgm;

Related

How to add a column to existing table in flask

Done as follows but no column is added.
Migrate database
python manage.py db migrate
Edit migrations/versions/{version}_.py
def upgrade():
from alembic import op
op.add_column('table_name', Column('column_name', INTEGER) )
Update schema
python manage.py db upgrade
The reason is that alembic stores version in table called alembic_version, and once {version} is in alembic_version, then nothing happens.
The solution is to create a new migration script and do migrate again.

django cron job cannot get model query

I am using django-crontab and the following cron job is working well:
the following cron job is added by
python manage.py crontab add
settings.py
CRONTAB_COMMAND_SUFFIX = '2>&1'
CRONJOBS = [
('*/1 * * * *', 'my_app.cron.test','>> ~/cron_job.log'),
]
my_app/cron.py
from datetime import datetime
def test():
print('HELLO : {}'.format(datetime.now()))
and once the server runs, it prints out to the log file:
~/cron_job.log
>...
>HELLO : 2018-01-04 23:52:02.983604
>...
same thing if I want to add a query for all my models :
my_app/cron.py
from datetime import datetime
from django.apps import apps
def test():
print('HELLO : {}'.format(datetime.now()))
print(apps.get_models())
~/cron_job.log
>...
>HELLO : 2018-01-05 10:00:02.283938
[<class 'django.contrib.admin.models.LogEntry'>, <class 'django.contrib.auth.models.Permission'>, <class 'django.contrib.auth.models.Group'>, <class 'django.contrib.auth.models.User'>, <class 'django.contrib.contenttypes.models.ContentType'>, <class 'django.contrib.sessions.models.Session'>, <class 'my_app.models.UserProfile'>, <class 'my_app.models.Post'>, <class 'my_app.models.Comment'>, ...]
>...
But when I start to query my model entries:
my_app/cron.py
from datetime import datetime
import blog_app.models
def test():
print('HELLO : {}'.format(datetime.now()))
for post in my_app.models.Post.objects.all():
print(post.title)
nothing is printed out. The model entries exist though.Any idea?
>...
>django.db.utils.OperationalError: no such table: blog_app_post
I struggled with this all day today and for me the problem was that Django wasn't connecting to the database through cron so it was defaulting to the sqlite database which was not migrated. The reason for this is that cron sets up a minimalistic environment and doesn't read the environment variables that you may have already had set. Most people using Django will setup their databases conditionally based on environment variables.
I solved the issue by giving cron access to the 'DATABASE_URL' environment variable that I needed. I found two ways of doing so. One way is to export the env variables that you need to the global /etc/environment file:
env | grep DATABASE_URL >> /etc/environment
You can do this from your entrypoint.sh script. I tried this out myself and it worked for me.
The other solution that I found seems a little bit more complex. It's described here: https://roboslang.blog/post/2017-12-06-cron-docker/. I haven't tried it, but it looks like it should work.

How can I activate the unaccent extension on an already existing model

When I try to install the unaccent Postgres extension (through the postgresql-contrib package), everything works as per the below:
# psql -U postgres -W -h localhost
Password for user postgres:
psql (9.3.9)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
postgres=# CREATE EXTENSION unaccent;
CREATE EXTENSION
postgres=# SELECT unaccent('Hélène');
unaccent
----------
Helene
(1 row)
However, when I try to use with Django 1.8, I get following error:
ProgrammingError: function unaccent(character varying) does not exist
LINE 1: ...able" WHERE ("my_table"."live" = true AND UNACCENT("...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Using Postgresql 9.3 and Django 1.8.
A migration file needs to be manually made and applied.
First, create an empty migration:
./manage.py makemigrations myapp --empty
Then open the file and add UnaccentExtension to operations:
from django.contrib.postgres.operations import UnaccentExtension
class Migration(migrations.Migration):
dependencies = [
(<snip>)
]
operations = [
UnaccentExtension()
]
Now apply the migration using ./manage.py migrate.
If you'd get following error during that last step:
django.db.utils.ProgrammingError: permission denied to create extension "unaccent"
HINT: Must be superuser to create this extension.
... then temporarily allow superuser rights to your user by performing postgres# ALTER ROLE <user_name> SUPERUSER; and its NOSUPERUSER counterpart. pgAdminIII can do this, too.
Now enjoy the unaccent functionality using Django:
>>> Person.objects.filter(first_name__unaccent=u"Helène")
[<Person: Michels Hélène>]

How do I inspectdb 1 table from database which Contains 1000 tables

I got a schema which Contains 1000 tables,and many of them I don't need,
how can I just inspectdb the just tables that I need?
You can generate the model of a single table, running this command
python manage.py inspectdb TableName > output.py
This works also if you want to generate the model of a view
You can do it in the python console, or in *.py file:
from django.core.management.commands.inspectdb import Command
from django.conf import settings
from your_project_dir.settings import DATABASES # replace `your_project_dir`
settings.configure()
settings.DATABASES = DATABASES
Command().execute(table_name_filter=lambda table_name: table_name in ('table_what_you_need_1', 'table_what_you_need_2', ), database='default')
https://github.com/django/django/blob/master/django/core/management/commands/inspectdb.py#L32
You can do it by the following command in Django 2.2 or above
python manage.py inspectdb --database=[dbname] [table_name] > output.py
You can get the models of the tables you want by doing:
python manage.py inspectdb table1 table2 tableN > output.py
This way you can select only the tables you want.
You can generate model's python code and write to the console programmatically.
from django.core.management.commands.inspectdb import Command
command = Command()
command.execute(
database='default',
force_color=True,
no_color=False,
include_partitions=True,
include_views=True,
table=[
'auth_group',
'django_session'
]
)
set table=[] empty list to get all tables

How can I run django shell commands from a bash script

Instead of repeatedly deleting my tables, recreating them and populating with data in my dev env, I decided to create a bash script called reset_db that does this for me. I got it to whack the tables, recreate them. But it's not able to populated the tables with data from the django orm.
I try to do this by calling the django shell from the script and then running ORM commands to populate my tables. But it seems like the django shell commands are not running.
I tried running the django orm commands manually/directly in the shell and they run fine but not from within the bash script.
The errors I get are:
NameError: name 'User' is not defined
NameError: name 'u1' is not defined
NameError: name 'm' is not defined
Here is my script:
#!/bin/bash
set +e
RUN_ON_MYDB="psql -X -U user --set ON_ERROR_STOP=on --set AUTOCOMMIT=off rcamp1"
$RUN_ON_MYDB <<SQL # Whack tables
DROP TABLE rcamp_merchant CASCADE;
DROP TABLE rcamp_customer CASCADE;
DROP TABLE rcamp_point CASCADE;
DROP TABLE rcamp_order CASCADE;
DROP TABLE rcamp_custmetric CASCADE;
DROP TABLE rcamp_ordermetric CASCADE;
commit;
SQL
python manage.py syncdb # Recreate tables
python manage.py shell <<ORM # Start django shell. Problem starts here.
from rcamp.models import Customer, Merchant, Order, Point, CustMetric, OrderMetric
u1 = User.objects.filter(pk=5)
m = Merchant(u1, full_name="Bill Gates")
m
ORM
I'm new to both django and shell scripting. Thanks for your help.
You should look at creating a fixture to populate your db https://docs.djangoproject.com/en/dev/howto/initial-data/
You need to import User explicitly. The django package and a few other things are automatically imported, but not everything you might want.
Also, to avoid not know what to import, there are management commands. This will leverage your Django and Python. You can learn shell scripting later.
clearly seen in your mistakes is not recognized as a model class User django-admin maybe you lack some import or something like this
from django.db import models
User import from django.contrib.auth.models
, by the way In line
User.objects.filter u1 = (pk = 5)
I think I put
u1 = User.objects.filter (pk = 5). First ()
at the end.
Anyway, here I leave some threads that may be of help,
https://docs.djangoproject.com/en/dev/ref/django-admin/
http://www.stackoverflow.com/questions/6197256/django-user-model-fields-at-adminmodel
https://groups.google.com/forum/?fromgroups = #! topic/django-users/WrVp1DDFrX8
Hope this helps.