[Answer]-How to upload data in model through migrations in django?

1👍

For older versions of Django you can use fixtures, but for versions >= 1.7, the preferred way to go is a Data Migration:

https://docs.djangoproject.com/en/1.8/topics/migrations/#data-migrations

# -*- coding: utf-8 -*-
from django.db import models, migrations

def create_objects(apps, schema_editor):
    SuggestionTitle.objects.create(fa_id="foo", desc="bar")

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_objects),
    ]

0👍

You need to use a fixture, not a migration. Fixtures can be in JSON, XML, or YAML format. The Django documentation explains how to use fixtures.

In your case, a JSON fixture might look like:

[
  {
    "model": "YOURAPPNAME.suggestion_title",
    "fields": {
      "fa_id": "FOO",
      "desc": "BAR"
    }
  },
  {
    "model": "YOURAPPNAME.suggestion_title",
    "fields": {
      "fa_id": "BAZ",
      "desc": "QUX"
    }
  }
]

Once you create your fixture, you load it using the loaddata management command, e.g:

manage.py loaddata myfixture.json

👤Joseph

Leave a comment