开发者

How to provide SQL initial data when app label is used?

From the documentation:

Django开发者_C百科 provides a hook for passing the database arbitrary SQL that's executed just after the CREATE TABLE statements when you run syncdb. You can use this hook to populate default records, or you could also create SQL functions, views, triggers, etc.

The hook is simple: Django just looks for a file called sql/[modelname].sql, in your app directory, where [modelname] is the model's name in lowercase.

It's not that simple. This does not seem to work when my model is declared in a sub-module of the app, and uses the app label mechanism:

in myapps/foo/models/bar.py:

class Baz(models.Model):
   ...
    class Meta:
        app_label = "foo"

Providing myapps/foo/sql/baz.sql does not work, neither does foo_baz.sql.

Any ideas?


Not sure what you want to do, but you could possibly write a management module in your app that listens for the post_syncdb signal.

But if you just want to populate the DB, you're probably better off using fixtures: http://docs.djangoproject.com/en/dev/howto/initial-data/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜