83đź‘Ť
Look at this workaround, posted by Bernie Sumption to the Django developers mailing list:
If makemigrations has not yet been run, the “migrate” command treats
an app as unmigrated, and creates tables directly from the models just
like syncdb did in 1.6. I defined a new settings module just for unit
tests called “settings_test.py”, which imports * from the main
settings module and adds this line:MIGRATION_MODULES = {“myapp”: “myapp.migrations_not_used_in_tests”}
Then I run tests like this:
DJANGO_SETTINGS_MODULE=”myapp.settings_test” python manage.py test
This fools migrate into thinking that the app is unmigrated, and so
every time a test database is created it reflects the current
structure of models.py.
In Django 1.9, this situation is improved somewhat, and you can set the value to None
:
MIGRATION_MODULES = {“myapp”: None}
76đź‘Ť
Here is the end of my settings file :
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
TESTS_IN_PROGRESS = False
if 'test' in sys.argv[1:] or 'jenkins' in sys.argv[1:]:
logging.disable(logging.CRITICAL)
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)
DEBUG = False
TEMPLATE_DEBUG = False
TESTS_IN_PROGRESS = True
MIGRATION_MODULES = DisableMigrations()
based on this snippet
I disabled migrations only when tests are running
- [Django]-How to do math in a Django template?
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Getting the SQL from a Django QuerySet
34đź‘Ť
django-test-without-migrations adds a --nomigrations
flag to manage.py test
. Works like a charm.
- [Django]-How to set True as default value for BooleanField on Django?
- [Django]-How to solve "Page not found (404)" error in Django?
- [Django]-How to filter empty or NULL names in a QuerySet?
6đź‘Ť
From Django version 3.1 and up the correct way to do this is to use the MIGRATE
setting in the database settings dict. See also the documentation.
#settings.py
DATABASES = {
'TEST': {
'NAME': 'Foo',
'MIGRATE': False
}
}
- [Django]-Django: list all reverse relations of a model
- [Django]-Django QuerySet order
- [Django]-Get the list of checkbox post in django views
3đź‘Ť
Update: Never mind, this change was reverted before 1.10 final was released. Hopefully it will return in a future version.
Note that as of Django 1.10 this can be controlled by a test database setting.
MIGRATE
Default:
True
If set to
False
, Django won’t use migrations to create the test database.
- [Django]-Sending HTML email in django
- [Django]-How to do SELECT MAX in Django?
- [Django]-*_set attributes on Django Models
3đź‘Ť
I just figure out how to disable migrations after django 1.10,may be it could help for somebody. Here is link at git
class DisableMigrations(dict):
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
DATABASES = DisableMigrations()
MIGRATION_MODULES = DisableMigrations()
Migrations for django 1.10 has two part,please look at load_disk and recorder
The part of load_disk
for migrations model of app that be added at INSTALL_APP
And the part of recorder
for database connection
For the version before 1.9 we need set MIGRATION_MODULES={'do.not.migrate':'notmigrations'}
when you are running test
Now we need set it None like MIGRATION_MODULES={'do.not.migrate':None}
So if we do not want make migrations for any app, just extend a dict and return None
for getitem
function , and do the same at DATABASES
, that is the right thing you need to do
PS: For command, you need to specify --setting=module.path.settings_test_snippet
after test
PPS If you are working with pycharm
,do not set --settings
options at Run/Debug configurations
, just add path of settings_test_snippet.py
at Custom setting. That just be fine!!
enjoy
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-Iterate over model instance field names and values in template
- [Django]-Create a field whose value is a calculation of other fields' values
2đź‘Ť
https://gist.github.com/apollovy/22826f493ad2d06d9a9a22464730ce0b
MIGRATION_MODULES = {
app[app.rfind('.') + 1:]: 'my_app.migrations_not_used_in_tests'
for app in INSTALLED_APPS
}
- [Django]-Detect mobile, tablet or Desktop on Django
- [Django]-How to use subquery in django?
- [Django]-How do you configure Django to send mail through Postfix?
2đź‘Ť
For django 1.9 and up the answer of Guillaume Vincent does not work anymore, so here’s a new solution:
I’m using this snippet in my settings file, after the definition of the INSTALLED_APPS
if os.environ.get('TESTS_WITHOUT_MIGRATIONS', False):
MIGRATION_MODULES = {
app.split('.')[-1]: None for app in INSTALLED_APPS
}
It iterates over all installed apps and marks each as having no migration module. See the django docs for more information.
Using this snippet you can run your tests, setting the environment variable TESTS_WITHOUT_MIGRATIONS
, e.g.:
TESTS_WITHOUT_MIGRATIONS=1 ./manage.py test
- [Django]-Default value for user ForeignKey with Django admin
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-Django REST Framework – 405 METHOD NOT ALLOWED using SimpleRouter