0

First of all: please don't ask me: "Why would you need this?".

Now the question itself: I have several applications installed in INSTALLED_APPS. Database is now empty and I want to synchronise only some of apps. What's the simplest way to do this?

I know I can write my custom management command based on syncdb. But it's a shame syncdb is designed in such a way that I would have to copy/paste a large chunk of code, changing only one line. This is a reason I want to explore other possibilities.

mnowotka
  • 16,430
  • 18
  • 88
  • 134

4 Answers4

1

You can do managed = False in the Meta class of a model. When this attribute is set, syncdb won't manage the app's tables for you. You can read about it here.

Another way would be to write a custom router

class MyAppRouter(object):
    """A router to control all database operations on models in
    the myapp application"""

    def db_for_read(self, model, **hints):
        pass

    def db_for_write(self, model, **hints):
        pass

    def allow_relation(self, obj1, obj2, **hints):
        pass

    def allow_syncdb(self, db, model):
        if model._meta.app_label in ['myapp1', 'myapp2']
            return False
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Too much hassle - I would have to loop through all models in all apps, remember previous state of the flag, then restore it afterwards. Is's easier to write a custom command... – mnowotka May 22 '13 at 21:12
  • No, I'm calling syncdb in my custom command, which exports model data from one database to another. If in target DB there is no tables I want to create them by syncdb but I need to create tables only for the application I'm exporting. – mnowotka May 22 '13 at 21:19
  • Not quite. I wan't to create reusable application for migrations. You can specify source and target DBs as a parameter of new management command as well as application, which data should be migrated. You can't achieve it using routers. – mnowotka May 22 '13 at 21:34
  • @mnowotka: I have used [this](http://diegobz.net/2011/02/10/django-database-router-using-settings/) generic router with great success – Adam Lewis May 23 '13 at 02:03
0

You can install django-extensions, enable it and use it's useful commands like:

./manage.py sqldiff APP_NAME

It will print out a list of SQL commands that you can run to syncdb your apps by name.

Django-extensions docs here: https://django-extensions.readthedocs.org/en/latest/

MostafaR
  • 3,547
  • 1
  • 17
  • 24
0

This seems to work - python manage.py syncdb --database=NEW_APP_DB

chipmunk
  • 944
  • 8
  • 19
0

For launch migration by app there is app_label argument:

<app_label>: The specified app has its migrations run, up to the most recent migration. This may involve running other apps’ migrations too, due to dependencies.

Example

./manage.py migrate myapp

See Django's docs.

Zulu
  • 8,765
  • 9
  • 49
  • 56