MVC --Model edition to learn from 0 with prejudice only

For those of you who are exhausted with the MVC framework, why don't you remember it with your own judgment and prejudice? Of course my thinking will be distorted a lot, but I guarantee it will be an interesting experience. This time, I'll use Django as an example.

In one word, the core of MVC would be "HTML that works to some extent with logic divided into coded ER diagrams" Let's explain what's great about this guy, using Model as an example. View and Controller are certainly great technologies, but they are not comparable to the power of Model.

Try changing the schema with full scratch PHP

You belonged to the field of writing code in full scratch PHP My boss asks me to change the SQL schema In that case, you have to do the following:

  1. Update ER diagram in Excel
  2. Update the DB with raw SQL statements
  3. Check DB
  4. Perform the test (may be a manual test depending on the site)
  5. It's midnight. You can finally go home

For those who don't know the ER diagram, it's roughly a relational diagram of RDBMS. Making this in Excel is just a pain. Besides, it changes Raw SQL statements are legacy, not something you want to touch at this time I don't want to be able to check the DB test? Is it manual anyway? Please forgive me already By the time I finally got home, I had only sleep time left Let's get rid of such a crappy day

Demonstrate the power of Model

You belonged to a Django site My boss asked me to change the SQL schema In that case, you only need to do the following

  1. Try changing the schema in Django
  2. Rewrite the DB (called "migrate. Automatically generate something like a modern SQL statement and rewrite the schema)"
  3. Do the test
  4. Goodbye at noon

It's a lie, but unfortunately true for full scratch PHP writing All you have to do is rewrite the ER diagram code called Model, migrate it, and test it. Many people may find it difficult to hear "Migrate", but all you have to do is open the terminal and type the following two lines.

python manage.py makemigrations
python manage.py migrate

This alone changed the SQL schema. After that, you can get rid of it from the site just by testing

Try it out

Let's experience this wonderful feature

You were told by your boss, "Make a model for a simple TODO app." As a result of organizing, I realized that it is enough to have a table of users and TODO, and a foreign key.

Stop the torture of writing in full scratch PHP. Suddenly jump into Django.

First, you decided to write a Model As a result, I wrote the following code

models.py


from django.db import models

class User(models.Model):
    name = models.CharField(max_length=20)
    password = models.CharField(max_length=20)

class Todo(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.CharField(max_length=140)

Strictly wrong code (Django comes standard with the user's model, it's enough to pull it in, and the password isn't encrypted), but it's easy to explain and the boss's eyes are above all. It seems to be cheated, so it will be okay To explain the meaning of the code, the first line pulls Django's models function and inherits it. CharField is just a string type, ForeignKey is just a foreign key With just such a tiny code, your after 5 is guaranteed

Next, let's update the DB That said, it's easy to do. As I explained earlier

python manage.py makemigrations
python manage.py migrate

You will succeed in rewriting the DB with this alone After that, you can do an automatic test or a manual test and leave it from the field Let's watch anime in the free time. I recommend PriPara

Explanation of View and Controller

Working HTML and logic, that's it Write if in demand

Recommended Posts

MVC --Model edition to learn from 0 with prejudice only
Steps to learn & infer transformer English-Japanese translation model with CloudTPU
I tried to implement SSD with PyTorch now (model edition)
SNS Flask (Model) edition made with Flask
Create folders from '01' to '12' with python
Programming to learn from books May 7
Seq2Seq (2) ~ Attention Model edition ~ with chainer
I came up with a way to make a 3D model from a photo.
I tried to learn the angle from sin and cos with chainer
Reinforcement learning to learn from zero to deep
Python to remember only with hello, worlds
Convert from PDF to CSV with pdfplumber
Conditional branch to learn from Milk Boy
A memorandum to make WebDAV only with nginx
OpenAI Gym to learn with PD-controlled Cart Pole
I tried to learn logical operations with TF Learn
Learn to recognize handwritten numbers (MNIST) with Caffe
[Introduction to minimize] Data analysis with SEIR model ♬
AWS Step Functions to learn with a sample
Back up from QNAP to Linux with rsync
From Python environment construction to virtual environment construction with anaconda
How to Learn Kaldi with the JUST Corpus
Dare to learn with Ruby "Deep Learning from scratch" Importing pickle files from forbidden PyCall
I came up with a way to make a 3D model from a photo. 0 Projection to 3D space