How do I makemigrations on a stand alone Django app (ie one that is not part if any project).
For example after following: https://docs.djangoproject.com/en/1.8/intro/reusable-apps/
You can do it similar to how you do testing scripts for apps:
#!/usr/bin/env python
import sys
import django
from django.conf import settings
from django.core.management import call_command
settings.configure(DEBUG=True,
INSTALLED_APPS=(
'django.contrib.contenttypes',
'your_app',
),
)
django.setup()
call_command('makemigrations', 'your_app')
What I do is to create a mock project, containing only that app, then the process is as usual:
manage.py makemigrations myapp
Related
I have a working Django project. If I run it - everything works fine
Here is the structure of the project
titest_project
tibrains_app
tools
__init__.py
load_django.py
countries_add.py
apps.py
models.py
load_django.py
import sys
import os
import django
sys.path.append('/mnt/HDD/tibrains/03/titest_project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'titest_project.settings'
django.setup()
countries_add.py
import load_django
from tibrains_app.models import ShopCountry
When I run countries_add.py it gives the error - No module named 'decouple'. Though when I run the project itself - it works without errors.
What could be the problem?
I am trying to run a standalone Django scipt
import os, sys, django
proj_path = "/path/to/django-project"
import ipdb; ipdb.set_trace()
# This is so Django knows where to find stuff.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "boiler.settings")
sys.path.append(proj_path)
django.setup()
When i run It says
ImportError: cannot import name 'Celery' from 'celery' (/path/to/django-poject/boiler/celery.py)
My folder structure:
django-poject
-- boiler
-- __init__.py
-- settings.py
-- celery.py
-- manage.py
__init__.py
from .celery import app as celery_app
__all__ = ['celery_app']
celery.py
import os
from celery import Celery
import django
import sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'boiler.settings')
#This defines the celery app instance
redis = 'redis://:pass#localhost:6379/0'
app = Celery(dirname,
broker=redis,
backend=redis
)
I am able to run celery using
activate virtualenv
cd to django-poject
celery -A boiler worker --loglevel=debug
without any problems
But in standalone its creating problems
You have to name your celery.py something else. Like django_celery.py otherwise it won't work. Celery works fine without it that way, but you want to integrate in with django and like what Santhosh said, the absolute import of itself is giving you issues.
In your project's __init__.py you'll need something like:
from __future__ import absolute_import, unicode_literals
from your_path_to.django_celery import app as celery_app
__all__ = ('celery_app',)
I have installed in my virtualenv django-reg.-redux
then i have added 'registration' app in INSTALLED_APPS,
but when i type python manage.py migrate
What package i have missed?stacktrace here
You need to manually update the admin.py file inside the registration app.
Open up registration\admin.py and then use the following import:
from django.contrib.sites.requests import RequestSite
instead of the wrong import:
from django.contrib.sites.models import RequestSite
i am trying to import the client in django for testing. but when i do, i get this wierd error:
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
this is how i imported the client so that i could use it for testing. can someone explain this to me please.
Try this:
import os
import sys
sys.path.append('/home/username/www/site_folder')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
But replace project with folder name, where your settings.py is
The Client is looking for the settings.py. You could simply load the client by typing this in your project folder:
python manage.py shell
In Pycharm which I use, after following this Running Django tests in PyCharm
my problem was solved.
It's in the file > settings > Django Support, and then select the right settings.
Django apps that are meant to be redistributed don't have a manage.py (nor a settings.py since that is part of the project, not the app).
How does one run schemamigration --auto in this scenario? Do I need to have a minimal settings/manage.py as part of the app repository in order to do this? Is there a way to do this with django-admin.py?
You just need to add the app to your Installed_Apps in your settings.py
Then you can run ./manage.py schemamigration <app_name> --auto
If the app doesn't have any migrations you will want to run ./manage.py schemamigration <app_name> --initial first and then ./manage.py schemamigration <app_name> --auto from then on.
Just managed to get this working in one of my project. Here's the code that works for me:
import sys
from django.conf import settings
from django.core.management import call_command
if not settings.configured:
settings.configure(
ROOT_URLCONF='',
DEBUG=False,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test.db'
}
},
INSTALLED_APPS=(
'south',
'my_app',
)
)
if __name__ == "__main__":
call_command('schemamigration', 'my_app',
initial=len(sys.argv) > 1,
auto=len(sys.argv) == 0
The above script is saved as migrate.py and run with python migrate.py or python migrate.py i (the i can be anything, and it will use --initial instead of --auto if present). Obviously, you can do fancier command line option parsin, but this works for me.
EDIT: Updated the script, DATABASES key was missing. On this project, I used the same database for testing the code, so it's not an entirely arbitrary configuration.