title: Django How to Populate Table with Data Migration?
body: <p>I need to populate a database table, and according this <a href="https://stackoverflow.com/a/51227744/11876504">post</a> the proper way to do this is using a data migration.</p>
  
  <p>I have an aux function, lets call it <strong>get_my_values</strong> that returns a dictionary of dictionaries with the following shape:</p>
  
  <pre class="lang-py prettyprint-override"><code>{
      origin1: { key2: value2, key3: value3, ..., keyN: valueN },
      origin2: { key1: value1, key3: value3, ..., keyN: valueN },
      ...
      originN: { key1: value1, key2: value2, ..., keyN-1: valueN-1 },
  }
  </code></pre>
  
  <p>For e.g:</p>
  
  <pre class="lang-py prettyprint-override"><code>{
    'ABC': { 'DEF': 1, 'GHI': 2 },
    'DEF': { 'ABC': 1, 'GHI': 3 },
    'GHI': { 'ABC': 2, 'DEF': 3 },
  }
  </code></pre>
  
  <p>And the table that I want to populate have this shape:</p>
  
  <pre class="lang-py prettyprint-override"><code>ORIGIN | 'ABC'  | 'DEF' | 'GHI' |
   char  |  float | float | float |
  </code></pre>
  
  <p>So, following this <a href="https://simpleisbetterthancomplex.com/tutorial/2017/09/26/how-to-create-django-data-migrations.html" rel="nofollow noreferrer">post</a> I created the following migration:</p>
  
  <pre class="lang-py prettyprint-override"><code>from django.db import migrations
  from myapp.utils import get_my_values
  
  
  def populate_my_table(apps, schema_editor):
    Data = apps.get_model('myapp', 'Table')
    my_data_dict = get_my_values()
    for origin in my_data_dict.keys():
      # insert into Data.origin the current origin
      # if the Data.origin == Data.column, insert in this column the value 0
      # else, insert in each Data.column, the corresponding value from my_data_dict[origin]['column']
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('myapp', 'the_table_migration'),
      ]
  
      operations = [
        migrations.RunPython(populate_my_table),
      ]
  </code></pre>
  
  <p>As you can see, I wrote a pseudo-code to populate it. But i'm struggling to actually turn into real code.<br>
  In the end, I expect records like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>ORIGIN | ABC | DEF | GHI |
   ABC   | 0.0 | 1.0 | 2.0 |
   DEF   | 1.0 | 0.0 | 3.0 |
   GHI   | 2.0 | 3.0 | 0.0 |
  </code></pre>