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

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

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 full text search: TrigramDistance not working

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;

export data from Django database

how to copy records from one database to another django ?
I tried for the first database
python manage.py dumpdata material - indent = 1
material is the directory database
after ?
material.json ?
do I copy this file somewhere? in the second database ?
You can use this command to dump the data to a json file:
python manage.py dumpdata material --indent=1 > my_dir/material.json
And then this command to load it into the database:
python manage.py loaddata my_dir/material.json

How to get all my data from model to JSON? I need export and import my data

How to get all my data from model to JSON? I need export and import my data
Is there any command?
You can use dumpdata command to dump data in your table. By default it gives in JSON format.
You can do in command line, to print data on screen.
$ python manage.py dumpdata
As suggested by Rohan, you need the dumpdata command.
I generally do an app at a time, output to file, and add an indent to the output to make it more readable -
$ python manage.py dumpdata --indent 2 myapp > /path/to/myapp/fixtures/my_data.json
First create fixtures folder inside your app.
Dump all models data of the app:
python manage.py dumpdata --format=json --indent=4 [app_name] > [app_name]/fixtures/initial_data.json

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.