See traceback of loaddata command (when error) - python

I'm trying to load fixture:
python manage.py loaddata stock/fixtures/initial_data.json
But error occurred without traceback (I don't know which model is the problem):
ValueError: Problem installing fixture 'stock/fixtures/initial_data.json': The database backend does not accept 0 as a value for AutoField.
How get the traceback?

There's a --traceback option.
python manage.py loaddata stock/fixtures/initial_data.json --traceback
This isn't mentioned in the loaddata docs, it's part of the default command options.

Related

Flask-Migrate trying to migrate User model throws error

I added two new columns to my User model, and I want to migrate the changes. I wrote down exact notes for myself because I have done this before and it worked:
flask shell
from files import db
from files.models import User
Then, I run it and get:
flask db migrate -m "Message"
^
SyntaxError: invalid syntax
I updated Flask-Migrate and everything.
i had same problem and i recognized that i was using "flask shell" and when i changed to terminal i solved my problem

Getting error when trying to migrate in django

Hey I'm trying to migrate my project using the command python manage.py makemigrations in django and I keep getting this error:
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
I have already found similar answers on it but nothing has worked. I am currently in the virtual environment already and changing the command to python3 manage.py makemigrations also prompts the same error.
The raise-from statement, or exception chaining, is only supported since Python 3.
Please upgrade your Python version to 3.0 or above in order to run this manage.py.

Python Django Unknown Command 'SQL'

I'm following this tutorial http://www.sitepoint.com/building-simple-rest-api-mobile-applications/
I'm trying to run SQL by
$ python manage.py sql fishes
However it says
Unknown Command: 'sql'
If I type
$ python manage.py help
I get this, and SQL doesn't appear in the list.
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
Unfortunately that doesn't exist any more, however the command manage.py dbshell does.
Generally speaking however, you should try to use models, with load_data etc to preserve the data integrity (as validations etc may happen in models rather then relying on underlying data bits). Or write management commands for any clean up tasks.
Yes it is possible, using the inspectdb command:
python manage.py inspectdb
or
python manage.py inspectdb > models.py
to get them in into the file
This will look at the database configured in your settings.py and outputs model classes to standard output.
As Ignacio pointed out, there is a guide for your situation in the documentation.
Source : link

Unable to create superuser in django due to not working in TTY

I go through first django tutorial from djangoproject.com and at the very beginning of part 2, which is creating superuser when I run "python manage.py createsuperuser" I get the following message back:
Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.
I get the same message when I go on to create superuser after running syncdb.
I am working on Eclipse for Windows 7, and Django 1.7.1 together with Python 2.7.8.
When using the Git Bash and to correct the above error message try to append winpty i.e. for example:
$ winpty python manage.py createsuperuser
Username (leave blank to use '...'):
You can create a superuser using django shell (python manage.py shell)
from django.contrib.auth.models import User
User.objects.create_superuser(username='YourUsername', password='hunter2', email='your#email.com')
if you are in virtualenv, cd into your virtualenv and activate it. then try these steps:
python manage.py syncdb --noinput
python manage.py migrate
python manage.py createsuperuser
Use "Windows PowerShell" or "Windows Cmd" and then use same command. Git command interface has some restriction.
I am a Windows10 user. I tried to run py manage.py createsuperuser command using Git Bash console, but error has been thrown. Then I switched Git Bash to native Windows Command Line with administrator privileges, and re-run command - it was working.
First run
$ django-admin startproject mysite
in cmd prompt,then apply migration by
cd mysite
mysite:
python manage.py makemigrations
then
python manage.py migrate
after that
python manage.py createsuperuser
From Django 3.0 you can do it without TTY
DJANGO_SUPERUSER_USERNAME=admin DJANGO_SUPERUSER_PASSWORD=psw \
python manage.py createsuperuser --email=admin#admin.com --noinput
also, you can set DJANGO_SUPERUSER_PASSWORD as the environment variable
If you are Windows user using GitBash terminal and trying to create super for admin it won't work instead of that use command prompt in administrative privilege it works
Gitbash terminal error
$ python manage.py createsuperuser
Superuser creation skipped due to not running in a TTY. You can run "manage.py createsuperuser" in your project to create one manually.
Error Resolved Using Command Prompt
python manage.py createsuperuser
Username (leave blank to use 'user'): admin
Email address:
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
This might be helpful for others. Do Upvote for this if it works for you
Use this command :
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
python manage.py runserver
Your error is probably:
[Error `You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, content types, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)`][1]
check you yo directory with Tree command:tree
Then run Make migration :
enter image description here
then create superuser with the python3 manage.py createsuperusercommand :

django deployment using fabric - TypeError: prepare_deployment() takes exactly 1 argument (0 given)

I'm using this guide:
http://www.jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/
To set up my django project. But I'm stuck at deployment part.
I installed fabric using pip in my virtualenv.
I created this file inside myproject directory:
from fabric.api import local
def prepare_deployment(branch_name):
local('python manage.py test finance')
local('git add -p && git commit')
local('git checkout master && git merge ' + branchname)
from fabric.api import lcd
def deploy():
with lcd('home/andrius/djcode/myproject/'):
local('git pull /home/andrius/djcode/dev/')
local('python manage.py migrate finance')
local('python manage.py test finance')
local('/my/command/to/restart/webserver')
But when I enter this command (as shown in a guide):
fab prepare_deployment
I get this error:
Traceback (most recent call last):
File "/home/andrius/env/local/lib/python2.7/site-packages/fabric/main.py", line 732, in main
*args, **kwargs
File "/home/andrius/env/local/lib/python2.7/site-packages/fabric/tasks.py", line 345, in execute
results['<local-only>'] = task.run(*args, **new_kwargs)
File "/home/andrius/env/local/lib/python2.7/site-packages/fabric/tasks.py", line 121, in run
return self.wrapped(*args, **kwargs)
TypeError: prepare_deployment() takes exactly 1 argument (0 given)
So even though it didn't specify in guide to enter argument, I suppose it requires my branch name. So entered this:
fab prepare_deployment v0.1
(v0.1 is my branch name)
So now I got this error:
Warning: Command(s) not found:
v0.1
Available commands:
deploy
prepare_deployment
Also I noticed in a guide for file fabfile.py in function prepare_deployment, input is written as 'branch_name' and inside function there is argument 'branchname'. So I thought it should be the same and renamed 'branchname' to 'branch_name', but still getting the same error.
So I think I'm doing something wrong here. What could be the problem?
Update:
I tried to call function inside fabfile.py with:
prepare_deployment("v0.1")
Output was this:
[localhost] local: python manage.py test finance
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
Type 'yes' if you would like to try deleting the test database 'test_finance', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: database "test_finance" does not exist
Fatal error: local() encountered an error (return code 2) while executing 'python manage.py test finance'
Aborting.
I think I should also mention that my app name is 'finance' as database name 'finance'. Maybe those are conflicting?
fabric uses a specific syntax for passing arguments to tasks from the command line. From a bash shell you would need to use
fab prepare_deployment:v0.1
You can read about it in the fabric documentation on per task arguments.
If you ever actually need to use brackets in a bash command you would need to escape them.
As you stated, this function should look like this...
def prepare_deployment(branch_name):
local('python manage.py test finance')
local('git add -p && git commit')
local('git checkout master && git merge ' + branch_name)
Then you simply call...
fab prepare_deployment("v0.1")

Categories

Resources