In Django, how to create tables from an SQL file when syncdb starts

How to get syncdb to execute SQL queries (to create tables) defined by me, and not automatically generate tables.

I am looking for this solution, as some models in my application represent SQL table tables for a legacy database table. So, I created my SQL views in my django-DB as follows:

CREATE VIEW legacy_series AS SELECT * FROM legacy.series;

I have a reverse engineering model that represents the above view / legacy. But whenever I run syncdb, I must first create all the views by running sql scripts, otherwise syncdb just creates tables for them (if the view is not found).

How to get syncdb to run the above SQL?

+5
source share
2 answers

There are two possible approaches that I know to adapt your models to the old database table (without using views):

1) Run python manage.py inspectdb in your project. This will create models for existing database tables, after which you can continue to work with them.

2) . , , db_table . -, , db_column. , db_, , .

, , sql . "" /sql/ "model".sql. Django sql . DROP , . , , django ( .sql , , ).

+5

SQL- .

. , syncdb , . , django Model , .

, Series :

class Series(models.Model):
    # model fields...
    ...

    class Meta:
        managed = False
        db_table = "legacy_series"

SQL- yourapp/sql/series.sql:

### yourapp/sql/series.sql
CREATE VIEW legacy_series AS SELECT * FROM legacy.series;

syncdb .

+4

All Articles