[PYTHON] WEB application development using Django [Model definition]

Continued from template addition

Model definition

The function called posts that we are currently creating is based on the image of a function for inputting articles such as blogs, so a database that stores titles, articles, dates, and images is required. I will add this to models.py.

models.py


from django.db import models

#We will define it as a class called Post.
class Post(models.Model): 

    #Definition for title, use CharField for string type
    #max_It is possible to limit the number of characters by length
    title = models.CharField(max_length=100)

    #Define date and time, use DateTimeField for date data
    published = models.DateTimeField() 

    #Definition of image data, use ImageField for image data
    #You can specify the save destination of the image data as an argument (this time, specify the media folder)
    image = models.ImageField(upload_to='media/') 

    #TextField is used for definition of sentences and text type
    #Used for long sentences
    body = models.TextField()

Next, execute the command to create the database table based on models.py.

$ python3.6 manage.py makemigrations

Migrations for 'posts':
  posts/migrations/0001_initial.py
    - Create model Post

If there is a new definition file, it will create a file to put into the database.

Then read the newly created file and create a table.

$ python3.6 manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
  Applying posts.0001_initial... OK

Access the database and check if the table exists.

$ sqlite3 db.sqlite3

SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .tables
auth_group                  django_admin_log          
auth_group_permissions      django_content_type       
auth_permission             django_migrations         
auth_user                   django_session            
auth_user_groups            posts_post #Newly created table
auth_user_user_permissions

You can see that a table called "posts_post" has been created.

Recommended Posts

WEB application development using Django [Model definition]
WEB application development using Django [Django startup]
WEB application development using Django [Application addition]
WEB application development using Django [Initial settings]
WEB application development using Django [Request processing]
WEB application development using Django [Template addition]
WEB application development using django-Development 1-
WEB application development using Django [Admin screen creation]
WEB application development using django-Development environment construction-
Application development using SQLite with Django (PTVS)
Development of WEB application using Django [Add data from management screen]
Web application using Bottle (1)
(Python) Try to develop a web application using Django
Web application development with Flask
Web application creation with Django
Creating a web application using Flask ②
Web application development memo in python
Build a web application with Django
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Application development using Azure Machine Learning
Try using the web application framework Flask
Get a reference model using Django Serializer
Web application development in Go language_Hands-on day 1
About Cloud run + Firebase development [Web application development]
I made a WEB application with Django
How to build an application from the cloud using the Django web framework
Django model: ManyToManyField
Until Django application creation by terminal (development environment)
Creating an interactive application using a topic model
First Django development
Build a Django development environment using pyenv-virtualenv on Mac
Django tutorial (blog application creation) ② --model creation, management site preparation
Web application made with Python3.4 + Django (Part.1 Environment construction)
[Django] Implement image file upload function without using model
Tech-Circle Let's start application development using machine learning (self-study)
Prepare Django development environment using homebrew on MacOSX Mavericks (10.9)
Development digest with Django
Django python web framework
Model changes in Django
High Performance Django --Model
Web App Development Practice: Create a Shift Creation Page with Django! (Design of database model)
Try using Django templates.html
[Memo] Django development environment
Try using the Python web framework Django (2) --Look at setting.py
Create an easy-to-use follow model in Django using ManyToManyField through