Deploying Custom SQL Views with Django - python

I've written some custom PostgreSQL views, which I've wrapped with Django's ORM using the managed=False meta flag. What's the best way to deploy these views, so that running syncdb updates them in the database, and also that the views exist in the test database when I run unittests?

With an initial SQL file.

Related

How to change model and update database, make migrations, in flask-admin?

I already have my flask-admin app with its models and database and tables created..
Now I would like to add a field to model and that should be reflected in the database by a new column in the model's corresponding table.
With django one would create then apply migrations..
How do I make migrations/schema-changes in flask-admin?
I would recommend checking out the section on SQLAlchemy-based database migrations in Miguel Grinberg's Database Flask Mega-Tutorial. He uses low-level SQLAlchemy APIs to put the database under version control and automate the process of database version upgrades or downgrades.
Alternatively, I recommend using Miguel Grinberg's Flask-Migrate extension which implements the functionality he describes in the aforementioned tutorial. Using this extension, you simply add an instance of the Migrate object to your application, then perform database migrations using a command line interface provided by the extension.

Python restful API tools selection when DB tables already created

I used Django REST framework to write API, the Django ORM is really conveinent like DB migrate or relation field's query.
But now we have a case, there is a DB with about 30 tables with data in it. And we have to query the DB and write some API. So I think I can't use Django this time.
What develop tools do you recommand to write restful API in Python if your case have the database that already created tables in it?
You can use inspectdb command to create django models from existing database.
python manage.py inspectdb > models.py
Here is the docs: Integrating Django with a legacy database

select data from a database out of my django app

How I can select data from a database that is not inside my django app?
How I should connect my django query to a database that has not been created by my django app?
I have put this code in views.py:
def select(request):
p = user.objects.filter(country='Iran')
return HttpResponse(p)
If the database had been created by django app then there was need to add this part into views.py:
(for example)
from users.models import user
But here what should I do?
P.S:
I'm using pymysl in my django project and have installed lamp in my linux os, and using phpmyadmin. Any more explanation is needed?
Django supports several databases in one instance.
Also you can manually connect to DB in your python code.
For example:
import psycopg2
psycopg2.connect(...)
According to the comment you made on the other answer,
First version of our project is in php, we want to migrate to python-django and use the same old database. So we can't create databases again and we should configure our django project to work with existing db.
You want to use an already existing legacy database in django. Django offers the possibility to create models out of your old db (legacy-databases).
Configure the db settings of your old database. Then you can run inspectdb to create django models of your old database:
python manage.py inspectdb > models.py

Django app as database web based UI

I'm planning to develop web UI something like iSQL*Plus-oracle but, for most common databases. Just take input query from user and return result with save options,etc.
So, for connecting to external data bases, what is advisable way,
1. Using django model and raw sql or,
2. with modules outside django - sqlalchemy+mysqldb,psycopg..?
Going through django documentation my understanding is db connections has to be in settings.py and I could not add from user input. Is my understanding is true or not?
I'm new to django not to python.
An ORM (something like Django's models or sqlalchemy) is a really helpful abstraction to help map tabular data in the database to the objects its being used to model in your code. It won't help with connecting to databases provided by the user since you won't know what the schema of the database is you're connecting to, nor what you are going to receive back from a query.
With django, the database defined in settings.py is used to store information related to your app such as user credentials, migrations as well as whatever else you define in your models.py files. So definitely don't try to change that dynamically as it is being used to store the state of your application for all users.
If you need to connect to external databases and run user-supplied queries, you can do that inside a view using the appropriate database driver. So psycopg2 for postgres would be fine.

To add model and access database in Booktype

I am trying to access the database in Booktype. I am using sqlite3. I am not able to view the tables. And even don't know how to create a new model.
How do I access and add model to the booktype?
you could use sqliteman to view your sqlite3 database
you should learn basic django docs, models
when run python manage.py syncdb, django will create tables in database (configuration in settings.py)

Categories

Resources