Django does not install model tables - python

In my project, I have a routers.py with different router classes. Now, I am making a new app. I have created my models.py. I have also registered the app in the INSTALLED_APPS in settings.py. Then, I ran validate. Everything is fine. When I syncd thoughb, Django does not install the tables. I tried using
python manage.py sqlall <app_name> | psql <database>
Then, I get an error message saying:
psql: FATAL: password authentication failed for user <user name>
I noticed that the role does not exist in postgres. So, I created the role with login privilege createdb and password. Then, I get a different error message:
psql: FATAL: password authentication failed for user <user name>
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
And, it does not provide the original exception.
Any help is much appreciated.

It looks like the Django application is unable to log onto the DB.
In django's settings.py make sure you have the proper DB credentials setup:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': get_env_variable("DJANGO_DB_HOST"),
'NAME': get_env_variable("DJANGO_DB_NAME"),
'USER': get_env_variable("DJANGO_DB_USER"),
'PASSWORD': get_env_variable("DJANGO_DB_PWD"),
'PORT': '',
},
...
}
As you can see my credentials are grabbed from environment variables. You can hardcode them in for test purposes.
Then in the DB (mine is postgresql) create the user/grant it the correct privileges, for example:
ssh root#dbhost
su - postgres
createdb dbname
psql
GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser
That should do.
I recommend the following steps as well, if they are missing from your setup:
In your app's admin.py register your models with Django's admin:
# Register your models here.
admin.site.register(Model1)
...
admin.site.register(ModelN)
Then, assuming you have created the project already, run:
python manage.py migrate
(it's the syncdb equivalent. Read the docs about migrations).
If that command does not ask for the admin superuser then create your administrative user (i.e. the user who can manipulate the models through django's admin interface) with:
python manage.py createsuperuser
Fire up Django
python manage.py runserver 0.0.0.0:8000
and see what happens whan you visit your site and admin at
localhost:8000/
localhost:8000/admin
Please pardon me if you know all those things already. That is what I normally do in my dev environment (I also use virtualenv, of course).

Related

How to browse django psql backend from command line?

I'm developing a website with Django 3 (in a docker container) using postgres sql as the backend; i.e. in the project settings file I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
I've populated the backend database and can browse the data using the admin. However, Id like to connect to the database via the command line so I can more easily test queries. I have tried connecting to the database the normal way from the command line:
sudo -u postgres psql
postgres=# \c postgres
The problem is that there is no data found:
postgres=# \dt
Did not find any relations.
Since I'm new to docker I thought to try connecting other ways as well; specifically, based on another post I tried:
sudo docker run -d -p 5432 -t postgres/postgresql /bin/su postgres -c '/usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf'
This throws an error:
pull access denied for psql/postgresql, repository does not exist or may require 'docker login'
Again, I'd like to connect to the database via the command line so I can more easily test queries. Perhaps Im on the right track but assistance would be appreciated.
It is a bad idea to use postgres for the database name, as there is a postgres database used for maintenance by default by PostgreSQL itself. I'd recommend calling the database something like my_project, then creating a service account user my_project_user, and assign a password:
sudo -u postgres psql
postgres=# CREATE USER my_project_user WITH PASSWORD 'blahblah';
postgres=# CREATE DATABASE my_project WITH OWNER my_project_user;
postgres=# \q
Update your Django DATABASES["default"] settings accordingly, and run migrations. After running migrations, you can access your Django database using the following management command:
python manage.py dbshell
You may be able to issue the command above with your current setup, but you may run into problems using postgres as your database name. Good luck!
UPDATE, my docker-compose.yml file had the following service:
db:
image: postgres:11
volumes:
- postgres_data:/var/lib/postgresql/data/
I am able to connect to the Django backend using the following command:
sudo docker-compose exec db psql -U postgres
postgres=# \c postgres
As suggested by #FlipperPA I will start useing a different username and database name
This command below runs(opens) the command-line client for PostgreSQL so that you can test queries:
python manage.py dbshell

Project created using cookiecutter-django not running

Its been a few months that I am trying to learn Django. In the same process (and while reading "Two Scoops of Django 1.11"), I came across Cookiecutter Django. It has helped me learn a few important things to keep in mind while creating a project.
I tried to run the template provided by cookiecutter-django but failed. Here are the steps that I followed.
Create a virtual environment named test and activate it.
mkvirtualenv test
Installed Cookiecutter.
pip install coockiecutter
Installed Cookiecutter Django, The project name was set to "Test Project" and other defaults settings were chosen. I am using PostgreSQL 9.6.
cookiecutter https://github.com/pydanny/cookiecutter-django
Create a database named "test_project" in PostgreSQL.
Run python manage.py migrate
The result was the error:
django.db.utils.OperationalError: FATAL: role "dev" does not exist
I have also tried making a user named test_project_user and granting it all the privileges to test_project database. I am still getting the same error.
The problem seems to be that you specified a database user that does not exist (or you left blank and it assumes your system user), in:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test_project',
'USER': 'HERE', # Set test_project_user here
...
}
}

"no such table" error on Heroku after django syncdb passed

I'm trying to deploy my Django application to Heroku. The migrations are in my local Git. When I try:
git push heroku master
heroku run python manage.py syncdb
It applies the migrations and also promts me to create superuser, which I successfully do. Now the application is up and running, however when I try to log into the Django admin it's throwing:
OperationalError no such table: user_user
When I try
heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
It applies all migrations, but fails to create superuser throwing:
django.db.utils.OperationalError: no such table: user_user
Either way I can not have my database set up and migrated on Heroku.
My database settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
My user model is:
class User(AbstractUser):
rating = models.PositiveIntegerField(default=settings.DEFAULT_USER_RATING)
Django version is 1.7.1.
How do I get my database tables created on Heroku?
You must not use sqlite3 on Heroku.
sqlite stores the database as a file on disk. But the filesystem in a Heroku dyno is not persistent, and is not shared between dynos. So, when you do heroku run python manage.py migrate, Heroku spins up a new dyno with a blank database, runs the migrations, then deletes the dyno and the database. The dyno that's running your site is unaffected, and never gets migrated.
You must use one of the Heroku database add-ons. There is a free tier for Postgres. You should use the dj-database-url library to set your database settings dynamically from the environment variables which Heroku sets.
Also, for the same reason, you must do manage.py makemigrations locally, commit the result to git, then push to Heroku.
You can use postgresql:
In settings.py add(at the end of file):
# ie if Heroku server
if 'DATABASE_URL' in os.environ:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
In requirements.txt add:
dj-database-url
psycopg2
Now you can run:
heroku run python manage.py migrate
pip install django-heroku
Add import django_heroku at top of file settings.py
Place django_heroku.settings(locals()) at the very bottom of settings.py
It will automatically configure your db. You can learn more on their website
What version of django you are using..?
If you are using django>=1.7 you need to run migrate
After adding models you need to do
python manage.py makemigrations then python manage.py migrate
If your project already contain migrations you can directly run python manage.py migrate command.
If you miss any step mentioned above please do that.

How is python manage.py createsuperuser useful?

I am a new in Django. I have found this piece of code
python manage.py createsuperuser
How is this useful? In what kind of situation is it necessary?
From the django-docs (emphasis mine):
This command is only available if Django’s authentication system (django.contrib.auth) is installed.
Creates a superuser account (a user who has all permissions). This is useful if you need to create an initial superuser account or if you need to programmatically generate superuser accounts for your site(s).
When run interactively, this command will prompt for a password for the new superuser account. When run non-interactively, no password will be set, and the superuser account will not be able to log in until a password has been manually set for it.
python manage.py createsuperuser
in addition to mu's answer, superuser is the one who can log into admin page and who can have permissions to add, edit, delete objects in thru admin page.
Whenever you wipe your database or deploy to a server you often want to be able to setup up an "admin" account to be able to log into the /admin/ interface like this:
python manage.py createsuperuser -u admin -e admin#example.com --noinput
python manage.py changepassword admin
Unfortunately, usually you want your deployments to be automatic and as #mu pointed out you still must interactively set the password. However, you can just add the record to the database yourself if you want to automate the process:
echo "from django.contrib.auth.models import User" > createadmin.py
echo "User.objects.create_superuser('dan', 'dan#email.io', 'pass')" >> createadmin.py
python manage.py shell < createadmin.py
Of course don't forget to set your password to something other than 'pass'.
#hobs thanks for this awesome answer. it pointed me in the right direction with regards to finding an ultimate answer that worked for me. Below is my working config, the only difference is I modified the User import for newer versions of Django
from django.contrib.auth import get_user_model
User = get_user_model()
User.objects.create_superuser('adminuser', 'dev#email.io', 'password')
and the command I run is;
python3 manage.py shell < /opt/admin.py
I needed this for a Dockerfile config and below is how the line looks in Dockerfile line
RUN \
python3 ./manage.py migrate && \
python3 manage.py shell < adminuser.py
thanks again and hopefully someone else finds this useful

Heroku Django db postgres error

I am trying to get my first django app up, but I'm running into this db error:
OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
This happens whenever I try to syncdb, migrate, or createsuperuser
I am configuring my DATABASES variable like:
DATABASES = {'default' : dj_database_url.config(default=os.environ["HEROKU_POSTGRESQL_OLIVE_URL"]) }
Is there something else I need to configure or am doing wrong?
EDIT (SOLVED):
Thanks for helping me narrow down the problem, I've found the solution.
Since this was the first time I deployed to heroku and the first time I used the two scoops django directory format. I thought doing something like
python manage.py syncdb # would be okay
instead beause my settings folder looks like
.../settings
base.py
local.py
production.py
demo.py
# ...
I need to do
python manage.py syncdb --app.settings.demo
Your syntax seems right, try using DATABASE_URL after verifying you promoted HEROKU_POSTGRESQL_OLIVE_URL, although it should work even when not promoted.
$ heroku pg:promote HEROKU_POSTGRESQL_OLIVE_URL
and then:
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
This setup should be working. Else, check that you are working on the right settings file. You can verify this by running:
$ heroku run python manage.py shell
and then:
>>> from django.conf import settings
>>> print settings.DATABASES['default']
and verify result.
I had an error in .gitignore and my local settings were getting set by Heroku by mistake.
This will also cause this error message message.

Categories