Skip to content

Instantly share code, notes, and snippets.

@timpamungkas
Created November 8, 2022 13:25
Show Gist options
  • Save timpamungkas/41e1c046d4d92f2350dbb0d7462b858f to your computer and use it in GitHub Desktop.
Save timpamungkas/41e1c046d4d92f2350dbb0d7462b858f to your computer and use it in GitHub Desktop.
<ol><li><p>You will use Django fixtures, a feature to upload data from JSON. On Django workspace under folder <em>myapp</em>, create folder with name <strong><em>fixtures</em></strong></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-21-d9fde732955de2be0fb1489751dc1bdb.png" width="300px"></figure><p> </p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-21-94bc3cb7fe582809bf8128af3297f264.png"></figure><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-21fb95f7b3eea3c8e6e64738650f96fe.png" width="300px"></figure></li><li><p>Download initial JSON&nbsp;data file from task's asset and upload it into Django workspace, folder fixtures</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-1dbdcb6470cac6c0049954ad37f77730.png" width="300px"></figure><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-6674542f827d20f074bf179f7d9c8323.png"></figure><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-4c1438816da47158b20f58df452e0868.png" width="300px"></figure></li><li><p>Open file <em>car_manufacturers_initial_data.json</em> </p><p>Django fixtures requires JSON&nbsp;array where each array element will be inserted as one row. The JSON&nbsp;must have following structure</p><pre class="prettyprint linenums">[
{
"model":"[django-app-name].[django-model-name]",
"pk":"[primary-key-value-1]",
"fields":{
"[field-name-1]":"[field-value-1]",
"[field-name-2]":"[field-value-2]",
...
}
},
{
"model":"[django-app-name].[django-model-name]",
"pk":"[primary-key-value-2]",
"fields":{
"[field-name-1]":"[field-value-1]",
"[field-name-2]":"[field-value-2]",
...
}
},
...
...
]</pre><p>The JSON provided by data engineer almost the same with JSON&nbsp;format required by Django fixtures, you need to modify it to conform the structure</p></li><li><p>For each element, add <code>"model":&nbsp;"myapp.CarManufacturer"</code></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-22c540f8938dccbc7f521dce127bfb8a.png"></figure><p>Notice several things :</p><ol><li><p><em>Toyota </em>has these data : <code>primary key</code>, <code>partnership_end_date</code>, <code>partnership_level</code></p></li><li><p><em>Ford</em> does not has <code>primary key</code> nor <code>partnership_end_date</code>, only has <code>partnership_level</code></p></li><li><p><em>Ford</em> does not has <code>primary key</code>, <code>partnership_end_date</code>, nor <code>partnership_level</code></p></li></ol><p>These fields are defined with argument <code>default</code> on Django data model, so when you load data using fixtures, default value will be used</p></li><li><p>Load car manufacturer data using command <code>python3 manage.py loaddata car_manufacturers_initial_data.json</code></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_14-20-22-450c2a331d257aa012d7edf965ca7060.png"></figure></li><li><p>Go to MySQL&nbsp;shell using terminal command <code>python3 manage.py dbshell</code> and check that three manufacturers data inserted. Use SQL statement <code>select * from car_manufacturers;</code></p><p>Notice the green rectangles below, where default value is used, according to Django data model</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_21-22-13-7ee7c6128a1b6c01bfb5d974f0b71594.png"></figure></li><li><p>Exit the MySQL shell using <code>exit</code></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-04_01-19-22-72720f82427bb8f77171a0ed6b7641c0.png"></figure></li></ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment