diff -r d205a9ab4b3b django/core/management/commands/loaddata.py
a
|
b
|
|
8 | 8 | from django.core import serializers |
9 | 9 | from django.core.management.base import BaseCommand |
10 | 10 | from django.core.management.color import no_style |
11 | | from django.db import connections, transaction, DEFAULT_DB_ALIAS |
| 11 | from django.db import connections, router, transaction, DEFAULT_DB_ALIAS |
12 | 12 | from django.db.models import get_apps |
13 | 13 | from django.utils.itercompat import product |
14 | 14 | |
… |
… |
|
31 | 31 | make_option('--database', action='store', dest='database', |
32 | 32 | default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load ' |
33 | 33 | 'fixtures into. Defaults to the "default" database.'), |
34 | | make_option('-e', '--exclude', dest='exclude',action='append', default=[], |
35 | | help='App to exclude (use multiple --exclude to exclude multiple apps).'), |
36 | 34 | ) |
37 | 35 | |
38 | 36 | def handle(self, *fixture_labels, **options): |
39 | 37 | using = options.get('database', DEFAULT_DB_ALIAS) |
40 | | excluded_apps = options.get('exclude', []) |
41 | 38 | |
42 | 39 | connection = connections[using] |
43 | 40 | self.style = no_style() |
… |
… |
|
171 | 168 | try: |
172 | 169 | objects = serializers.deserialize(format, fixture, using=using) |
173 | 170 | for obj in objects: |
174 | | if obj.object._meta.app_label not in excluded_apps: |
| 171 | if router.allow_syncdb(using, obj.object): |
175 | 172 | objects_in_fixture += 1 |
176 | 173 | models.add(obj.object.__class__) |
177 | 174 | obj.save(using=using) |
diff -r d205a9ab4b3b django/core/management/commands/syncdb.py
a
|
b
|
|
16 | 16 | make_option('--database', action='store', dest='database', |
17 | 17 | default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' |
18 | 18 | 'Defaults to the "default" database.'), |
19 | | make_option('-e', '--exclude', dest='exclude',action='append', default=[], |
20 | | help='App to exclude (use multiple --exclude to exclude multiple apps).'), |
21 | 19 | ) |
22 | 20 | help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." |
23 | 21 | |
… |
… |
|
26 | 24 | verbosity = int(options.get('verbosity', 1)) |
27 | 25 | interactive = options.get('interactive') |
28 | 26 | show_traceback = options.get('traceback', False) |
29 | | exclude = options.get('exclude', []) |
30 | 27 | |
31 | 28 | self.style = no_style() |
32 | 29 | |
… |
… |
|
59 | 56 | created_models = set() |
60 | 57 | pending_references = {} |
61 | 58 | |
62 | | excluded_apps = set(models.get_app(app_label) for app_label in exclude) |
63 | | included_apps = set(app for app in models.get_apps() if app not in excluded_apps) |
64 | | |
65 | 59 | # Create the tables for each model |
66 | | for app in included_apps: |
| 60 | for app in models.get_apps(): |
67 | 61 | app_name = app.__name__.split('.')[-2] |
68 | | model_list = models.get_models(app, include_auto_created=True) |
| 62 | model_list = models.get_models(app, include_auto_created=True, using=db) |
69 | 63 | for model in model_list: |
70 | 64 | # Create the model's database table, if it doesn't already exist. |
71 | 65 | if verbosity >= 2: |
… |
… |
|
101 | 95 | |
102 | 96 | # Install custom SQL for the app (but only if this |
103 | 97 | # is a model we've just created) |
104 | | for app in included_apps: |
| 98 | for app in models.get_apps(): |
105 | 99 | app_name = app.__name__.split('.')[-2] |
106 | 100 | for model in models.get_models(app): |
107 | 101 | if model in created_models: |
… |
… |
|
126 | 120 | print "No custom SQL for %s.%s model" % (app_name, model._meta.object_name) |
127 | 121 | |
128 | 122 | # Install SQL indicies for all newly created models |
129 | | for app in included_apps: |
| 123 | for app in models.get_apps(): |
130 | 124 | app_name = app.__name__.split('.')[-2] |
131 | 125 | for model in models.get_models(app): |
132 | 126 | if model in created_models: |
… |
… |
|
145 | 139 | transaction.commit_unless_managed(using=db) |
146 | 140 | |
147 | 141 | from django.core.management import call_command |
148 | | call_command('loaddata', 'initial_data', verbosity=verbosity, exclude=exclude, database=db) |
| 142 | call_command('loaddata', 'initial_data', verbosity=verbosity, database=db) |
diff -r d205a9ab4b3b django/db/models/loading.py
a
|
b
|
|
4 | 4 | from django.core.exceptions import ImproperlyConfigured |
5 | 5 | from django.utils.datastructures import SortedDict |
6 | 6 | from django.utils.importlib import import_module |
| 7 | from django.db import router |
7 | 8 | |
8 | 9 | import sys |
9 | 10 | import os |
… |
… |
|
132 | 133 | self._populate() |
133 | 134 | return self.app_errors |
134 | 135 | |
135 | | def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False): |
| 136 | def get_models(self, app_mod=None, include_auto_created=False, |
| 137 | include_deferred=False, using=None): |
136 | 138 | """ |
137 | 139 | Given a module containing models, returns a list of the models. |
138 | 140 | Otherwise returns a list of all installed models. |
… |
… |
|
143 | 145 | |
144 | 146 | By default, models created to satisfy deferred attribute |
145 | 147 | queries are *not* included in the list of models. However, if |
146 | | you specify include_deferred, they will be. |
| 148 | you specify include_deferred=True, they will be. |
| 149 | |
| 150 | By default, if a model is defined, and matches the other arguments, |
| 151 | it will be returned. However, if a using argument is specified, only |
| 152 | models that are synchronized onto the named database will be returned. |
147 | 153 | """ |
148 | | cache_key = (app_mod, include_auto_created, include_deferred) |
| 154 | cache_key = (app_mod, include_auto_created, include_deferred, using) |
149 | 155 | try: |
150 | 156 | return self._get_models_cache[cache_key] |
151 | 157 | except KeyError: |
… |
… |
|
160 | 166 | model_list.extend( |
161 | 167 | model for model in app.values() |
162 | 168 | if ((not model._deferred or include_deferred) |
163 | | and (not model._meta.auto_created or include_auto_created)) |
| 169 | and (not model._meta.auto_created or include_auto_created) |
| 170 | and (using is None or router.allow_syncdb(using, model))) |
164 | 171 | ) |
165 | 172 | self._get_models_cache[cache_key] = model_list |
166 | 173 | return model_list |
diff -r d205a9ab4b3b django/db/utils.py
a
|
b
|
|
120 | 120 | if allow is not None: |
121 | 121 | return allow |
122 | 122 | return obj1._state.db == obj2._state.db |
| 123 | |
| 124 | def allow_syncdb(self, db, model): |
| 125 | for router in self.routers: |
| 126 | allow = router.allow_syncdb(db, model) |
| 127 | if allow is not None: |
| 128 | return allow |
| 129 | return True |
diff -r d205a9ab4b3b docs/ref/django-admin.txt
a
|
b
|
|
423 | 423 | have specified that you want to load data onto the ``master`` |
424 | 424 | database. |
425 | 425 | |
426 | | Excluding applications from loading |
427 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
428 | | |
429 | | .. versionadded:: 1.2 |
430 | | |
431 | | The :djadminopt:`--exclude` option may be provided to prevent specific |
432 | | applications from being loaded. |
433 | | |
434 | | For example, if you wanted to exclude models from ``django.contrib.auth`` |
435 | | from being loaded into your database, you would call:: |
436 | | |
437 | | django-admin.py loaddata mydata.json --exclude auth |
438 | | |
439 | | This will look for for a JSON fixture called ``mydata`` in all the |
440 | | usual locations - including the ``fixtures`` directory of the |
441 | | ``django.contrib.auth`` application. However, any fixture object that |
442 | | identifies itself as belonging to the ``auth`` application (e.g., |
443 | | instance of ``auth.User``) would be ignored by loaddata. |
444 | | |
445 | 426 | makemessages |
446 | 427 | ------------ |
447 | 428 | |
diff -r d205a9ab4b3b docs/topics/db/multi-db.txt
a
|
b
|
|
66 | 66 | $ ./manage.py syncdb --database=users |
67 | 67 | |
68 | 68 | If you don't want every application to be synchronized onto a |
69 | | particular database. you can specify the :djadminopt:`--exclude` |
70 | | argument to :djadmin:`syncdb`. The :djadminopt:`--exclude` option lets |
71 | | you prevent a specific application or applications from being |
72 | | synchronized. For example, if you don't want the ``sales`` application |
73 | | to be in the ``users`` database, you could run:: |
74 | | |
75 | | $ ./manage.py syncdb --database=users --exclude=sales |
| 69 | particular database, you can define a :ref:`database |
| 70 | router<topics-db-multi-db-routing>` that implements a policy |
| 71 | constraining the availability of particular models. |
76 | 72 | |
77 | 73 | Alternatively, if you want fine-grained control of synchronization, |
78 | 74 | you can pipe all or part of the output of :djadmin:`sqlall` for a |