I created many test accounts in my heroku server. Now they all have empty columns, and therefore do not work. And now I want to remove all data about existing users/accounts, including superuser, then recreate. How I can do this?
You can go to your terminal, where you have executed the command 'python manage.py runserver' and execute the command - 'python manange.py shell', then your shell will start.
Then execute the command:-
step-1: from django.contrib.auth.models import User
step-2: User.objects.all().delete() # this will delete all the user from your table
step-3: exit() # exit from the shell
step-4: python manage.py createsuperuser # for creating the superuser
This is totally in server side.
Go to your heroku database and reset database from the setting.
after that.
$ heroku run python manage.py makemigrations
$ heroku run python manage.py migrate
Create New Admin ( can be any or the same your existing)
All done, 100% that works for me.
Related
I'm following a tutorial that uses Django's authentication system to log in users and when the sqlite3 db is first connected to django and the command python manage.py syncdb (tutorial is from before syncdb was deprecated) is run, I don't get this same message in the command line:
You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now?
I instead ran
python manage.py makemigrations
python manage.py migrate
and this seemed to have set up the tables
but then when I run:
select * from auth_user;
nothing is returned to me in the command line.
How can I setup a superuser from command line and get encrypted password, etc?
I want to delete a Django UserModel table and then recreate it.
Or delete user field and recreate it with a new user by python manage.py createsuperuser
[NOTE]:
My DB is PostgreSQL on a docker container.
connect to psql or using pgAdmin
connect to your databese
execute the query:
DROP TABLE IF EXISTS CASCADE;
Are you trying to delete a development database (e.g., during local development?).
In that case, I usually like to just unapply and reapply migrations, making any changes to migrations I need before reapplying them.
E.g.,:
$ python manage.py migrate auth zero
$ python manage.py migrate auth
This would destroy the auth tables (Permission, Group, and User) and recreate them without any data.
If you have other migrations that depend on auth, they will have been rolled back too. In that case you can just migrate the whole app back with:
$ python manage.py migrate
I am trying to move my django project into a production environment and in doing so I switched from using sqlite to postgres. In my development environment, whenever I made changes to my models or anything that would significantly change how the database was setup, I would literally just drag my sqlite file to the trash and just run syncdb to create a new empty one (probably bad practice). Now that I am using postgres, I am wanting to do the same thing without actually deleting the database. Basically I was wondering if there was a way to completely empty it or clear it out and then just run syncdb and start over?
I also welcome any alternative suggestions that might lead me down the right path, I'm very new to this.
You can use flush. Just run this command:
python manage.py flush
First if you have initial data in your database you can use dumbpdata command:
python manage.py dumpdata > initial_data.json
For specific app run:
python manage.py dumpdata <app_name> > initial_data.json
Second run the flush command to clean your database:
python manage.py flush
Third and last, run loaddata command to load the initial data into your database and create superuser by running createsuperuser command
python manage.py loaddata initial_data.json
python manage.py createsuperuser
In case flush does not work, you can drop the whole database.
Go to windows command line.
If server is 'postgres' and db name is 'mydb', run:
C:\> psql -U postgres
You will see a postgres-# prompt. Next is to close connections running the following:
SELECT * FROM pg_stat_activity WHERE pg_stat_activity.datname='mydb';
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';
Drop database once for all: DROP DATABASE mydb;
So I am a newbie to Django and I had added a field to a model.py and that created issues in Django.
I learned I needed a migration tool, and used south. Turns out south has issues with sqlite. So I configured Django settings for MYSQL. I can add data to the DB (MYSQL)
I deleted the db.sqlite3 file , but it comes back after ever syncdb.
When I run syncdb it says:
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
Synced:
> django.contrib.admin
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.messages
> django.contrib.staticfiles
> south
Not synced (use migrations):
- accounts
(use ./manage.py migrate to migrate these)
So my questions are:
1. Changing to MYSQL did not remove the mid migration that south failed on, as I hoped it would. What are my options now in order to deal with this
Do I even have to do anything, or can I keep working on my app or is this mid migration something to address? I ask because the new MYSQL DB seems to have the new field I added, which was why I wanted to migrate in the first place. So you can see my confusion...my migration broke, but now with MYSQL, the fields are fine, but Django still thinks im in the middle of a migration. Why is this and what is the recommendations?
thanks
I have fought many many many (many) times with Databases & Migrations & South... I hope this information helps you to fix your issues and save you some hours of hard work
Migrating accounts app
I have a question here, is accounts the name of your app ? Or is it an external app ?
The accounts app seems to be managed by South, that means that It won't be synced with the command python manage.py syncdb, you will need to migrate it by yourself with:
# This command generates the migrations file
python manage.py shchemamigration accounts --initial
# This command use the migrations file and apply the changes to the DB
python manage.py migrate accounts
# --initial is used only in the INITIAL migration
# If you modify the models on the accounts app and want to migrate again
# You will need to
python manage.py shchemamigration accounts --auto
python manage.py migrate accounts
Keep an eye on your migrations
If you check the folder migrations following the standard structure it is yourapp/yourapp/migrations
If you delete your database, remember to delete your migrations files, is better to start over unless you need to save the Database changes
Make sure that your Database and your migrations are synchronized, if they are synchronized add some changes to the database will be easy
Some times, although you're using south, when you do python manage.py syncdb your app will be synced, I will explain which problems can come from here:
Your app models are created in the database with the command python manage.py syncdb
After 1. you do python manage.py schemamigration --initial and it will create the initial migrations
Now, if you try to do python manage.py migrate yourapp it will fail because the tables has already been created, so you need to fake the initial migration with:
python manage.py migrate yourapp --fake
and from here you could use south without problems
How to "restart" the database
This process will empty the database and delete all the migrations, take care if you have some data you want to save
Sometimes you will mess with the migrations, and depending on the issue, it can be faster to drop the database and start the migrations again:
python manage.py dbshell
# Next 3 lines inside the MySQL console
>> drop database yourappdatabase
>> create database yourappdatabase
>> quit
# Delete all the migrations within the migrations folder
rm yourapp/yourapp/migrations/00*
python manage.py syncdb
python manage.py schemamigration yourapp --initial
python manage.py migrate yourapp
How to save informations from the database
Some times when you're starting a new app you will delete/create the database many times, and to avoid typing again and again data on the models, it could be useful to create migrations from some models with:
python manage.py dumpdata yourapp.ModelName > file.json
To reload this data again you just need to:
python manage.py loaddata file.json
How would you run this django command to syncdb with fabric automatically.
python manage.py syncdb --settings="app.settings.test"
if tried to do run, it gets stuck at the "Do you want to create superuser account", can it passed as yes and login information with it.
run('python manage.py syncdb --settings="app.settings.%s"' % name, pty=True)
Add --noinput to the arguments to keep django-admin from prompting:
python manage.py syncdb --settings="app.settings.%s" --noinput
If you have specific credentials that you'd like to preload always, I suspect the simplest way to achieve that would be to create a data dump of the user database from a machine with (just!) the admin account loaded, and then to load that in after syncdb. Alternatively, you could simply leave out the admin account and add it later with manage.py createsuperuser when and if you need to have it.