heroku run python manage.py makemigrations -a myapp takes forever - django

I have added this to my setting.py
DATABASE_URL = os.environ.get('DATABASE_URL')
db_from_env = dj_database_url.config(default=DATABASE_URL)
DATABASES['default'].update(db_from_env)
and yet when I run
heroku run python manage.py makemigrations -a myapp
it stucks in connecting process. Does anyone know how to solve this?

Related

heroku createsuperuser does not create a user. Does not make entry in auth_user table

(cfehome) C:\Dev\cfehome\verojewels>heroku run python manage.py
createsuperuser
Running python manage.py createsuperuser on ⬢ verojewels... up, run.7672
(Free)
Username (leave blank to use 'u48267'): shruti
Email address: shruti.karva#gmail.com
Password:
Password (again):
Superuser created successfully.
after this also, no entry in auth_user table is made
Execute following command to initialize database.
heroku run python manage.py migrate
Also don't forged to add Heroku Addon for database and correctly configure your Django project in settings.py.
If you are using Postgres, please check if you have something like following and DATABASES['default'] is not overridden.
import dj_database_url
DATABASES['default'] = dj_database_url.config()

Schema migration in django south

I am trying to use schema migration as i have my models in which i had two models previously and i have created three more models.
I have tried several commands like
python manage.py schemamigration appname --add modelname
python manage.py schemamigration appname
python manage.py schemamigration
python manage.py schemamigration appname --fake
but no luck
sometimes it gives me the error
You have not passed any of --initial, --auto, --empty, --add-model, --add-field or --add-index.
and sometimes this one
sage: manage.py schemamigration [options]
Creates a new template schema migration for the given app
manage.py: error: ambiguous option: --add (--add-field, --add-index, --add-model?)
i am new to django so want some help.
Finally got it
python manage.py schemamigration appname --auto

Django create superuser from batch file

I'm trying to make a batch file that runs syncdb to create a database file, and then create a superuser with the username "admin" and the password "admin".
My code so far:
python manage.py syncdb --noinput
python manage.py createsuperuser --username admin --email admin#exmaple.com
python manage.py runserver
Now this prompts me to enter a password and then confirm the password. Can I enter this information with a command from the same batch file, another batch file, or is this just not possible?
As it seems you can't provide a password with the echo ''stuff | cmd, the only way I see to do it is to create it in Python:
python manage.py syncdb --noinput
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin#example.com', 'pass')" | python manage.py shell
python manage.py runserver
As of Django 3.0 (per the docs) you can use the createsuperuser --no-input option and set the password with the DJANGO_SUPERUSER_PASSWORD environment variable, e.g.,
DJANGO_SUPERUSER_PASSWORD=my_password ./manage.py createsuperuser \
--no-input \
--username=my_user \
--email=my_user#domain.com
or, using all environment variables:
DJANGO_SUPERUSER_PASSWORD=my_password \
DJANGO_SUPERUSER_USERNAME=my_user \
DJANGO_SUPERUSER_EMAIL=my_user#domain.com \
./manage.py createsuperuser \
--no-input
And if as is good practice you are using a custom user (CustomUser in this case)
echo "from django.contrib.auth import get_user_model; CustomUser = get_user_model(); CustomUser.objects.create_superuser('me', 'nt#example.co.uk', 'mypwd')" | python manage.py shell

Django, Celery, RabbitMQ - Tasks are not been executed

I have setup my Django env like this:
INSTALLED_APPS = (
....
'djcelery',
)
BROKER_URL = "amqp://guest:guest#localhost:5672//"
CELERY_IMPORTS = ('bulksms.tasks', 'premiumsms.tasks', 'reports.tasks')
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
import djcelery
djcelery.setup_loader()
In the Django admin I add the task I want to periodic tasks (I can see all the tasks there) to run every minute for testing purposes but the task never runs.
Running django then
python manage.py celeryd -E --loglevel=DEBUG
python manage.py celerycam
In the admin site under Djcelery there is also no option for Tasks to ADD (not sure if it used to be there.)
If you want to run periodic tasks with Celery then you need to run the celerybeat process with either
python manage.py celery beat
or using another thread in the worker process
python manage.py celery worker -E --loglevel=DEBUG -B
See the docs on starting the scheduler http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#starting-the-scheduler

django redistributable app with south

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.