Reputation: 45
Added models from existing code. I'd like to put in the initial data when I migrate the models I've added.
python3 -m pip install sqlparse
python3 manage.py makeemigations sbimage
//I have edited the generated 0002 file.
python3 manage.py Migrate image 0002
//Normal operation confirmed.
python3 manage.py sqlmigrate thimage 0002
//Normal operation confirmed.
However, the data did not enter the table when the database was verified.
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sbimage', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='AuthNumberR',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('auth_number_r', models.CharField(max_length=64)),
],
),
migrations.RunSQL("INSERT INTO AuthNumberR (id, auth_number_r) VALUES (2, 'c');"),
]
Upvotes: 1
Views: 2148
Reputation: 4635
I think you should not use naked SQL queried, instead try using something like this
from django.db import migrations
def combine_names(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
Person = apps.get_model('yourappname', 'Person')
for person in Person.objects.all():
person.name = '%s %s' % (person.first_name, person.last_name)
person.save()
class Migration(migrations.Migration):
dependencies = [
('yourappname', '0001_initial'),
]
operations = [
migrations.RunPython(combine_names),
]
Reference : https://docs.djangoproject.com/en/2.2/topics/migrations/#data-migrations
Upvotes: 1
Reputation: 1847
You can use django fixtures to provide initial data to your database. It is quite useful for your case.
Upvotes: 2