I'm trying to have a purely in-memory SQLite database in Django, and I think I have it working, except for an annoying problem:
I need to run syncdb before using the database, which isn't too much of a problem. The problem is that it needs to create a superuser (in the auth_user table, I think) which requires interactive input.
For my purposes, I don't want this -- I just want to create it in memory, and I really don't care about the password because I'm the only user. :) I just want to hard-code a password somewhere, but I have no idea how to do this programmatically.
Any ideas?
Any inputs or prompts of django-admin.py may be suppressed using the --noinput option, such as
./manage.py syncdb --noinput
This will skip the super user creation on the initial syncdb run and thus requires you to create one either using fixtures or an custom method as receiver of the post_syncdb signal.
See django-admin.py and manage.py in the Django docs:
--noinput
Use the --noinput option to suppress
all user prompting, such as "Are you
sure?" confirmation messages. This is
useful if django-admin.py is being
executed as an unattended, automated
script.
Disconnect django.contrib.auth.management.create_superuser from the post_syncdb signal, and instead connect your own function that creates and saves a new superuser User with the desired password.
You will want to use fixtures:
https://docs.djangoproject.com/en/1.3/howto/initial-data/
Related
I have Django installed,but I was trying to create my first URL and I typed the following using the command prompt
"cd myproject" then again I typed "manage.py runserver 127.0.0.1:8000" And I got the following output:
"System check identified no issues<0 silenced>
you have 17 unapplied migration. Your project may not work properly until you apply the migrations for app: admin,auth,contenttypes,sessions."
Please what do I do to fix this problem so that my project can work properly?
Note that: I am new in using Python and Django
You already have an advice to read the tutorial but in case you want to continue on your own (before next trouble), you need to run this in console in order to apply migrations:
python manage.py migrate
Read more about migrations and this command here.
You need to migrate the database first. But it's not error actually. It's just a warning. If you don't want to migrate(create DB tables) just ignore it. And you can still browse 127.0.0.1:8000
Migration command
python manage.py migrate
Read Django documentation very carefully. Django Official Documentation
I want to automate the python manage.py makemigrations as in if a user encounters Did you rename game.last to game.las (a CharField)? [y/N] then the input will always be y but if the user encounters You are trying to add a non-nullable field 'las' to game without a default then it will automatically and continuously enter 1.
I tried yes | python manage.py makemigrations as researched however this will just throw an infinite loop of Please select a valid option if the default input is asked
My desire is the automation between 1 and y value as mentioned on my first paragraph or just throw an error if I input a wrong option on the default input
First, run manage.py makemigrations locally. It makes a script to modify the database schema and that is part of development and places it in the project/migrations directory. Build the container such that the generated migrations are included in your Docker container (they should be automatically as they are part of the Django project source tree), and run manage.py migrate --noinput on start-up of the container. It will automatically apply any migrations that haven't been applied. Your migrations build up over time but you can squash them if you REALLY need to, I would recommend against it except rare circumstances.
This way, you will keep your development and production database schema in sync, and won't run into strange bugs because you generated new migrations on production that didn't exist in your development environment.
I would recommend purposefully designing your migrations so that they don't require any input on manage.py migrate. That is not always possible, for instance, stale models will sometimes require input and just be left there unless manually deleted.
Since all of the Django source is available to you, it's trivial to make a version of the migrate command that assumes y and place it as a management command. Then run that management command instead of migrate, done.
I would show an example of this but it is specific to the version of Django you are running.
I actually solved the problem via timeouts like timeout 30 yes | python manage.py makemigrations so if in case an infinite loop via wrong option selection on non-nullable field input returns. It will automatically exit after 30 seconds. Giving me an alternative somehow to proceed my CI/CD without pushing my migrations
I've renamed some models in Django and created the migrations using python manage.py makemigrations.
When using python manage.py migrate, I get prompted with the following message
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel:
On my local machine, I can simply type 'yes'. However,
my application is deployed on Heroku and I have configured migrations to run automatically when the application is built. I achieve this using a post_compile file that looks like this:
# Run Django migrations
./manage.py migrate
# Compress static assets
./manage.py compress
Will the migration simply fail to complete as a consequence of not being in an interactive shell (and therefore not being able to answer 'yes' to this question)? If so, how can this problem be avoided?
You can use the --noinput command line argument of migrate command:
./manage.py migrate --noinput
This would suppress the prompt, but will not delete stale content types (ie. it works as if you answered No at the prompt). See Django ticket #25036 .
Another alternative would be to use the Unix command yes (I am not sure if it is enabled on Heroku by default though):
yes "yes" | ./manage.py migrate
I have an application in Django 1.6.5. I have a model where I removed one field, I added another field, and the third upgraded. And when we now turn to the model in the admin panel, I get the message:
ProgrammingError at /admin/app/subscription/
column app_subscription.enabled does not exist
The command python manage.py syncdb does not work.
Django (hopefully) doesn't modify your database schema if you don't explicitely ask for it. The syncdb command works perfectly, but (as documented) it will only create tables that don't yet exists (and are not marked as being managed externally in your models).
So you have mostly three options here:
manually drop your table and re-run syncdb. This mean you will loose all our data, so it's hardly a "solution"
manually alter your database schema. You won't loose your data, but you'll have to repeat the same (manual) operation everywhere your app is deployed... If it's only installed on your local workstation that might be ok, else it's not a reliable professional production-level option.
Use South (which seems to be installed since you do have a migrate command available.
Note that solution #3 imply that you do create the migration files for your app, as documented here : http://south.readthedocs.org/en/latest/tutorial/part1.html#the-first-migration
It just happened that I faced the same issue with django 1.9.x, where I added a new field in my django app which triggered the same error as you mentioned above.
I logged into the dbshell environment using
python manage.p dbshell # I know some use ./manage.py
and dropped all of my tables by running the following command within the dbshell to drop the tables
your_psql=# drop schema public cascade;
This will drop all of your tables (be careful as you may lose your data, there away to keep the data!) and you will get a message right after executing this command tells you that all dropped. Right after that run the following command to create the schema again, otherwise your server will not run:
your_psql=# create schema public;
Then just do the
python manage.py makemigrations # you might not need this, and
python manage.py migrate
And you're ready to go.
I know this answer might be very late but I hope it will help someone.
Cheers
I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?
As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That's probably a better approach than the solution below.
Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data.
The methods below will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:
Django 1.4.15 and earlier
python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json
Django 1.5 and newer
python manage.py dumpdata <your_app> > temp_data.json
python manage.py sqlclear <your_app> | python manage.py dbshell
python manage.py syncdb
python manage.py loaddata temp_data.json
(The reset command was deprecated and then removed in Django 1.5)
If your changes break your old schema this won't work - in which case tools like South or Django Evolution are great.
As of Django 1.7, you can now do this with native migrations. Just run
python manage.py makemigrations <your app name>
python manage.py migrate
Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.
note: syncdb can't update your existing tables. Sometimes it's impossible to decide what to do automagicly - that's why south scripts are this great.
Django's syncdb doesn't alter existing tables in the database so you have to do it manually. The way I always do it is:
Change the model class first.
Then run: manage.py sql myapp.
Look at the sql it prints out and see how it represented the change you are going to make.
Make the change manually using your database manager.
Check to see if everything worked correctly using the admin site.
If you are using sqllite a good manager is the firefox plugin: link
Another tool would be django evolution. No table dropping needed in most cases.
django evolution
Just install it as any other django app and run:
python manage.py evolve --hint --execute
deseb is a great tool for that.
Having it installed, you can write ./manage.py sqlevolve and it'll generate sql commands necessary to keep the database structure in sync with your models.
You need to drop your tables before you can recreate them with syncdb.
If you want to preserve your existing data, then you need to unload your database,
drop your tables, run syncdb to build a new database, then reload your old data into your new tables.
There are tools that help with this. However, in many cases, it's just as easy to do it manually.
For versions 1.4.1 and above users the command has changed to
python manage.py flush
Please read the official document before using it as it will delete all your data.