I was working on an existing Django project (with an old version) and needed to modify some of existing models to add some new fields in the table (including some many to many fields). Definitely http://south.aeracode.org/ is the right way to deal such schema and data migrations, however this was not the option for me due to some constraints.
Following is one of the sample model, I had in my project:
name = models.CharField(max_length=120)
slug = models.SlugField(max_length=150)
categories = models.ManyToManyField("BusinessCategory",related_name='allcategories',null=True)
price = models.FloatField(null=True, blank=True)
YEARLY = 'YR'
BIANNUALLY = 'BA'
MONTHLY = 'MO'
HOURLY = 'HR'
NEGOTIABLE = 'NG'
PRICE_CHOICES = (
price_type = models.CharField(max_length=2,
default=YEARLY, null=True, blank=True)
achievements = models.TextField(null=True, blank=True)
years_of_experience = models.FloatField(null=True, blank=True)
trainers = models.ManyToManyField("self",related_name='trainers',null=True, blank=True )
According to the Django 1.7 release note https://docs.djangoproject.com/en/1.7/releases/1.7/ , Django now has built-in support for schema migrations. It allows models to be updated, changed, and deleted by creating migration files that represent the model changes and which can be run on any development, staging or production database. Again, this was of no use for me as I was working with a legacy system :(.
I was left with only one option, somehow I needed the raw SQL query which Django fires on database while creating a table in database. Looking into the python doc, I got what I needed. Django has a command which generates the raw SQL for the provided application, which works in the following way:
python manage.py sql business
Here, business was my Django application under my project. Hope it helps!!
Latest posts by Ankit Jaiswal (see all)
- Creating HTML5 forms elements in Django forms - June 9, 2015
- Generating raw SQL query for Django model definition to create table schema manually - December 5, 2014
- Django – Incorrect integer value: ‘True’ for column ‘is_superuser’ at row 1 - November 14, 2014