So this is my first project with Django. As a result, I seem to learn new things about how it handles models every day. Because of this, my database is changing every day. Each time I want to update my database, I need to drop it, create it, run syncdb, and then populate it with user data. I was hoping to automate this process with a Python script to save me the hassle of constantly recreating my database.
When I try to run an INSERT command, MySQL is telling me that the column first_name does not have a default value. I took a quick look at the source (https://github.com/django/django/blob/master/django/contrib/auth/models.py), and the class User definitely defines a default value for the first_name column, as well as many others. Does anyone now why I am getting this error?
Here is the relevant part of the script:
import MySQLdb, os
from django.core.management import call_command
os.environ['DJANGO_SETTINGS_MODULE'] = "DatingWebsite.settings"
db = MySQLdb.connect(user = "project", passwd = "project")
c = db.cursor()
try:
c.execute("DROP DATABASE projectdb;")
except:
pass
finally:
c.execute("CREATE DATABASE projectdb;")
call_command('syncdb', interactive=False)
c.execute("USE projectdb;")
c.executemany("""INSERT INTO auth_user (id, username, email, password)
VALUES (%s, %s, %s, %s)""",
[
(1, 'bob', 'bob@gmail.com', 'pbkdf2_sha256$10000$A3ww6NUZ8TmS$5UA2sXCMS6x7uIGKe20hDckmisBY/rGUK7aye/tMUIw='),
] )
Thank you for any help
You may be better of using django south so that you don’t have to recreate your DB every time.
However, if you want to do recreate the DB, you can try using fixtures to populate data DB.