开发者

Create or copy table dynamically, using Django db API

how can I perform smth like

CREATE TABLE table_b AS SELECT 开发者_开发技巧* FROM table_a

using Django db API?


As an alternative you could potentially leverage South since it has an API for creating and dropping tables.

http://south.aeracode.org/wiki/db.create_table

I haven't tried this outside the context of a migration so no guarantees it will work out-of-the-box.


Django's ORM isn't intended for things like this. I would suggest you're doing something the wrong way but you haven't explained why you want to do this, so I can't really comment.

Anyway. You can use Django's raw SQL:

def my_custom_sql():
    from django.db import connection, transaction
    cursor = connection.cursor()

    # Data modifying operation - commit required
    cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
    transaction.commit_unless_managed()

    # Data retrieval operation - no commit required
    cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    row = cursor.fetchone()

    return row

http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly


You can't. You'll have to drop to DB-API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜