在openshift上使用django+postgresql

时间:2023-03-09 19:30:30
在openshift上使用django+postgresql

openshift上用的是django 1.7,数据库选择的是postgresql 9.2

本地开发用的是sqlite3数据库,发布到openshift上后是没有数据的(本地的sqlite3数据库里的数据并没有添加到openshift的数据库里,因为服务端的是postgresql数据库)。那么怎么在openshift上操作postgresql数据库呢?Let's go!

一、连接openshift上的应用

$ rhc ssh myapp

二、激活python虚拟环境

source $OPENSHIFT_HOMEDIR/python/virtenv/venv/bin/activate

三、开启django shell

python "$OPENSHIFT_REPO_DIR"wsgi/manage.py shell

进入界面:

Python 3.3. (default, Mar  , ::)
[GCC 4.4. (Red Hat 4.4.-)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

在shell里就可以操作数据库了

>>> from django.db import connection
>>> cur=connection.cursor
>>> cur
<bound method DatabaseWrapper.cursor of <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object at 0x7f1fe2a175d0>>

给postgresql的数据表里添加一条记录:

>>> from myblog.models import Todolist
>>> q=Todolist(title='hello world')
>>> q
<Todolist: hello world>
>>> q.save()
>>> q
<Todolist: hello world>
>>> Todolist.objects.all()
[<Todolist: hello world>]

--End--