Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create database automatically in django using settings.py or models.py

Tags:

python

django

Current flow:

1) mysql >> CREATE {DATABASE} db_name  [create_specification] ;
2) change database info in settings.py
3) python manage.py syncdb (assuming we have ready-made models)

Isn't there a way to do the same without using step 1. Maybe putting database name and specifications somewhere in settings.py so that i don't have to manually configure the db everytime i shift this project to some server

EDIT - WHY I want to dodge the first step:

In my case, different columns of different tables have different collation types. So while development, whenever I recreate the database, i need to manually change the configurations of individual columns/tables, which is frustrating.

like image 962
suneet Avatar asked Apr 11 '14 09:04

suneet


People also ask

How does Django automatically create database?

What is this? Django doesn't create databases for you automatically. You have to do this yourself manually. This is a simple package that creates your database for you automatically, if necessary, when you run migrate for the first time.

What does settings py do in Django?

settings.py is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a bunch of other stuff.

What is the use of models py in Django?

Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.

Which is default database in settings file in Django?

By default, the configuration uses SQLite. If you're new to databases, or you're just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won't need to install anything else to support your database.


2 Answers

All you need is to know database user / password with grant create database, all other data is in settings. You can connect a custom command to pre_syncdb signal to gather this data.

Take a look to createsuperuser that is raised on post_syndb signal to learn about this.

EDITED

syncdb is not longer available. Since 1.9 you should to use pre_migrate signal.

like image 157
dani herrera Avatar answered Oct 01 '22 14:10

dani herrera


I don't think its possible to dodge the step one, at least if you're using a different database backend except SQLite in django.

A note from docs: https://docs.djangoproject.com/en/dev/intro/tutorial01/#database-setup

If you’re using PostgreSQL or MySQL, make sure you’ve created a database by this point. Do that with “CREATE DATABASE database_name;” within your database’s interactive prompt.

If you’re using SQLite, you don’t need to create anything beforehand - the database file will be created automatically when it is needed.

If you move your project to a server like Heroku, the db creation is automated, like when using PostgreSQL.

I'm wondering why you want to dogde the first step, however if you're desperate, you might still want to trying going the direct python way via psycopg2

Creating a postgresql DB using psycopg2

like image 20
KhoPhi Avatar answered Oct 01 '22 14:10

KhoPhi