Skip to content

Instantly share code, notes, and snippets.

@timpamungkas
Created November 8, 2022 13:23
Show Gist options
  • Save timpamungkas/3ca844d4e91f456df2e6276735782795 to your computer and use it in GitHub Desktop.
Save timpamungkas/3ca844d4e91f456df2e6276735782795 to your computer and use it in GitHub Desktop.
<ol><li><p>Create a Python data model with the name <strong>CarManufacturer</strong>. Edit file <em>myapp/models.py </em>and add the following code</p><pre class="prettyprint linenums">class CarManufacturer(models.Model):
&nbsp; &nbsp; name = models.CharField(max_length=255)
&nbsp; &nbsp; origin_country = models.CharField(max_length=255)
&nbsp; &nbsp; description = models.TextField(max_length=4000)</pre><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-33-e2bbc68d358677718060cd50c8115f78.png"></figure></li><li><p>In the terminal, make sure you are in folder <em>~/code</em> by using the command <code>cd ~/code</code> </p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-33-73b1272f0e5dc51bde51a3d9d20d4b34.png"></figure></li><li><p>Create the automatic Python-to-MySQL database migration using Django. Use command <code>python3 manage.py makemigrations</code></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-33-b5343e94a99fe7d1ab2bdfe6748e023b.png"></figure><p>This command will create an automatic migration script, so you don't need to write a manual SQL statement. The migration script is located at folder <em>myapp/migrations</em>, but you don't need to modify it</p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-03_22-51-42-6d04093e2cea44270f3546d073f091f0.png" width="300px"></figure></li><li><p>You can go to MySQL shell by using command <code>python3 manage.py dbshell</code>. </p><p>Inside the MySQL shell, show existing tables by using command <code>show tables;</code>. Notice that it has some tables, but does not have anything related to the CarManufacturer model that you have just created. </p><p>To exit the MYSQL shell and go back to terminal, use <code>exit</code>. </p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-33-6f05d8c647f49459ff8c9ba685375fbd.png"></figure></li><li><p>From the terminal, apply the automatic migration script by using command <code>python3 manage.py migrate</code></p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-34-1c93c62de044a5f5036a08f263a237da.png"></figure></li><li><p>Use command <code>python3 manage.py dbshell</code> to go inside the MySQL shell, then show the existing tables by using command <code>show tables;</code></p><p>Notice that a new table created :&nbsp;<em>myapp_carmanufacturer</em>. </p><figure><img src="https://img-c.udemycdn.com/redactor/raw/create_lab_editor/2022-11-02_23-59-34-16176cd0a6506e7781097bf1b596984d.png"></figure></li><li><p>Describe the <em>myapp_carmanufacturer</em> table by using MySQL command <code>describe myapp_carmanufacturer;</code></p><p>Notice that it has same columns as the Python data model, as well as the auto-generated primary key column: <code>id</code> which is created by Django.</p><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-02_23-59-34-84190a426b8bd88033c3b878ea4b08e9.png"></figure></li></ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment