Django superuser fixture error - no such table: auth_user - python

I want to define a fixed username and password for superuser creation in Django's syncdb (after it has been executed). The method I'm using below was working in an older version of Django (I guess 1.6) but now it's not working.
I have this fixture file initial_data.json :
[
{
"fields": {
"username": "me",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": null,
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$...",
"email": "a#a.co",
"date_joined": "2015-04-23T01:13:43.233Z"
},
"model": "auth.user",
"pk": 1
}
]
when I add this in settings.INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
)
and run python manage.py syncdb, I get the following error :
django.db.utils.OperationalError: Problem installing fixture 'path/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user
What should I do?
Can I change the order that fixtures load to ensure that auth_user table is created before this fixture is loaded?
Or any other way to automatically create a superuser in Django?
Thanks in advance.

I solved my problem, however it is definitely not the prettiest solution.
I saw that when I run heroku run python manage.py syncdb, I get the following
Operations to perform:
Synchronize unmigrated apps: myproject, permissions, myapp1, myapp2
Apply all migrations: auth, flatpages, admin, contenttypes, sessions, sites
Synchronizing apps without migrations:
Creating tables...
Creating table app1_model1
Creating table app1_model2
...
So I thought what if i migrate the included apps in reverse order :
heroku run python manage.py migrate sites; heroku run python manage.py migrate sessions; heroku run python manage.py migrate contenttypes; heroku run python manage.py migrate admin; heroku run python manage.py migrate flatpages; heroku run python manage.py migrate auth;
and it worked! I have no idea why, but it did. Maybe this will help someone else too.

Related

Django makemigrations does not create admin databases

I have a django project, this is the installed apps entry in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crawler',
]
when I run python manage.py migrate everything seems to be fine. But when I try to login into django admin page, It says
(1146, "Table 'stock_db.django_session' doesn't exist")
It seems to me that migrate is not creating django admin databases, but why?
I even tried deleting the database, creating it again and running python manage.py makemigrations and python manage.py migrate, It didn't create django_session table.
Migrating your models won't create Admin resources. Those need to be created separately in your admin.py. See more details here
https://docs.djangoproject.com/en/4.1/ref/contrib/admin/
Essentially you need to create a Resource class, specify the fields, define whatever you want:
class MyModelResource(resources.ModelResource):
class Meta:
model = MyModel
fields = [
"your_fields",
]
Finally, you need to register your resource:
admin.site.register(MyModelResource)
Try makemigrations first if it doesnt help continue reading
This type of problem can appear in many situations, most common one is that your migration file system has broken. Reason may be that you tried to edit migration files, or you deleted some of them, or if you work on big teams there may be conflicting migrations. Fixing migrations system takes a lot of time and effort, plus you should know how they work. If project is not in production and you just playing around, I recommend to clear db and delete migration files and run makemigrations and migrate again.
Also be aware if you have multiple apps with models referencing each other with foreign keys or whatever.
Always make sure you run makemigrations before migrate
First run your migrations using below commands:
python manage.py makemigrations appname
python manage.py migrate
If this doesn't work then delete your current migrations using Python manage.py squashmigrations and then re-run migration commands.

Error when running migrate command Django

I am trying to deploy my first web project on a server. I am getting this error when running migrate and makemigrations:
ProgrammingError (1146, "Table '<user>$<dbname>.<table_name>'' doesn't exist")
Everywhere it says that you should run manage.py syncdb but this is obsolete , also when running manage.py --run-syncdb the same error is given.
models
class Book(models.Model):
name = models.CharField(max_length=350)
author = models.CharField(max_length=350)
category = models.CharField(max_length=200)
def __str__(self):
return self.name
snipppets from settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
} #Everything is replaced with the correct credentials
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main_app',
'accounts',
]
The DB is empty on the server. On my PC (when I developed it locally) it worked, it created the table for me when it didn't exist in the DB.
I am using Django 1.11 and Mysql 5.6.27.
I tried other answers but did not help me.
Can you please suggests what can I do to solve this issue?
You should use migrations mechanism: https://docs.djangoproject.com/en/1.11/topics/migrations/
./manage.py makemigrations # this creates migrations files
./manage.py migrate # this applies migrations
You don't need to use syncdb.
for the first time you creating your Django project in order to creating pre-installed apps (such as admin interface) database in django you need to use :
python manage.py migrate
it creates all necessary database tables and relations.
but every time you make changes to your app like creating new columns in your models, you can use:
python manage.py makemigrations [your app name]
the result will be something like below :
(env35) someone#shell:~/bookstore$ python manage.py makemigrations books
Migrations for 'books':
0005_publisher_family.py:
- Add field family to publisher
and finally you run:
python manage.py migrate
to Synchronizes the database state with the current set of models and
migrations.

Django Admin - login

I'm building a Django Web App with Django Suit for the administration interface.
Already got have Python 2.7, Django 1.10, and MySQL communicating in harmony and started a project:
python -m django-admin startproject webapp
So, those were the steps made in the Windows PowerShell after that:
1. Start our virtual env and run the server:
PS C:\WINDOWS\system32> cd C:\PythonProjects
PS C:\PythonProjects> virtualenvs\rrh\Scripts\activate
(rrh) PS C:\PythonProjects> cd rrh/webapp
2. Setup Django Suit
(rrh) PS C:\PythonProjects\rrh\webapp> pip install django-suit==0.2.23
Then,
go to settings.py file and add the 'suit' application:
(this is how is looking on mine)
INSTALLED_APPS = [
'suit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Also,
make sure you have django.template.context_processors.request,in your TEMPLATES OPTIONS context_processors (in settings.py):
This is required to handle left side menu.
(this is how is looking on mine)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request', # Make sure you have this line
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Then,
(rrh) PS C:\PythonProjects\rrh\webapp> ./manage.py collectstatic
(rrh) PS C:\PythonProjects\rrh\webapp> pip install git+https://github.com/darklow/django-suit.git
Django Suit (https://github.com/darklow/django-suit)
The folder structure is the following:
The database as the following aspect (with one row in auth_user):
This is the screen when run the command and view in the browser the result:
(rrh) PS C:\PythonProjects\rrh\webapp> python manage.py runserver
The problem is: even though there's one user in the DB, with username and password, why am I not able to do the login?
Thank you for the help
It appears you are trying to login on a part of your website which requires administrative rights.
Did you create the user with createsuperuser in ./manage.py shell?
created a new database, a new virtualenv, installed django, started a new django admin project, did migration, created a user, installed mysql-python, migrated to mysql, installed django suite, add the specifications to installed apps and all worked perfect.
see the django procedures I went through to make this here.

Heroku app.json set default django admin password

I'm trying to add a depoly to heroku button and I followed their examples and all and ended up with the following app.json for my django app
{
"name": "foo",
"description": "foo website",
"repository": "https://github.com/foo/bar",
"keywords": ["python", "django", "foobar"],
"env": {
"DJANGO_SECRET_KEY": {
"description": "A randomly generated secret to secure your Django installation",
"generator": "secret"
}
},
"scripts": {
"postdeploy": "sh -c 'python manage.py syncdb --noinput; python manage.py migrate --noinput'"
}
}
Doing the syncdb will create the auth tables and so on, but what I need is to allow the user who wants to deploy the app to specify the default admin username and password. I realize that doing heroku run python manage.py syncdb will prompt the user if we wants to create a superuser, but that's not exactly what I need. I need it to be configured from Heroku's dashboard when a user clicks on the button. I want this to allow non technical people to still be able to deploy the app without going through the terminal and all. Is there any way of doing this?
After some research, I managed to do what I wanted and thought I'd share it. I was able to do it using django-extensions. I made a python script
# create_admin.py
import os
if "ADMIN_USER" in os.environ and "ADMIN_PASSWORD" in os.environ:
from django.contrib.auth.models import User
user=User.objects.create_user(os.environ['ADMIN_USER'], password=os.environ['ADMIN_PASSWORD'])
user.is_superuser=True
user.is_staff=True
user.save()
and changed the scripts part of my app.json
"scripts": {
"postdeploy": "sh -c 'python manage.py syncdb --noinput; python manage.py migrate --noinput; python manage.py runscript create_admin'"
}
make sure you read django-extensions docs for more information on where to place the scripts and how to python manage.py runscript.
IMPORTANT: you may want to go to herkou's configs page and remove the environment variables after you run the script.

No migrations to apply

$python manage.py syncdb
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
the error printscreen
i don't know what the problem !
As the command line output says the problem is that you changed the models. The solution is to run python manage.py makemigrations and then python manage.py migrate.
The Best Thing You can do is, Delete the existing database.
In my case, I were using phpMyAdmin SQL database, so I manually delete the created database overthere.
Again run the following Commands:
python manage.py makemigrations
python manage.py migrate
python manage.py makemigrations <app_name>
python manage.py migrate
IT COULD BE BECAUSE YOU HAVE NOT YET REGISTERED YOUR APP IN SETTINGS.PY inside this file install your app (put the name of your app in quotes) after you can make python3 manage.py makemigrations and then python3 manage.py migrate (it can be python3 or python without the 3 ):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'new_app_name',
]
Hi my friend after all these steps if there was still this problem.
1- Clear all tables in the database.
2-Clear all history in the migrations folder of your project directory.
3-python manage.py makemigrations
4-python manage.py migrate

Categories