You are strongly advised to check corresponding YouTube video at the end of this article.
A model is a Python class that represents a database table. A Django model defines the fields and behaviors of the data that will be stored in a database, as well as the metadata required to manage the database table.
Django models are defined in a "models.py" file, typically located within a Django application. Each model is defined as a subclass of Django's "models.Model" base class, which provides it with a set of predefined methods and attributes for interacting with the database.
Models can define various types of fields, such as CharField for storing character strings, IntegerField for storing integers, DateField for storing dates, and so on. Additionally, models can specify various options such as default values, nullability, unique constraints, and more.
Once defined, Django models can be used to create database tables, query data from the database, and modify or delete existing data. Django's Object-Relational Mapping (ORM) provides a high-level, Pythonic interface for interacting with the database, allowing developers to work with data in a more natural and intuitive way.
Models are a crucial component of Django's framework for building web applications, allowing developers to easily define and manage the data structures that underlie their applications.
Navigate to models.py inside todo directory - we need to create structure of a database.
from django.db import models
from datetime import datetime
class Posts(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField()
created = models.DateTimeField(default=datetime.now)
The model inherits from Django's "models.Model" base class, which provides it with database-related functionality.
The "Posts" model has three fields defined: "title", "text", and "created".
The "title" field is a CharField with a maximum length of 200 characters, used to store the title of the post.
The "text" field is a TextField used to store the main text of the post.
The "created" field is a DateTimeField with a default value of the current date and time, used to store the date and time when the post was created.
This model could be used to represent a blog post in a Django application, allowing users to create and publish new posts, which would be stored in a database and displayed on the website.
Now, we need to prepare for migrations:
C:\todo_generic>python manage.py makemigrations
Migrations for 'todo':
todo\migrations\0001_initial.py
- Create model Posts
C:\todo_generic>
And than, migrations are possible:
C:\todo_generic>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, todo
Running migrations:
Applying todo.0001_initial... OK
C:\todo_generic>
Let's run our server:
C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 27, 2020 - 15:42:23
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
We need to be able to see new options in Django Administration
Go to admin.py inside todo directory, and put this there:
from django.contrib import admin
from .models import Posts
# Register your models here.
admin.site.register(Posts)
Now we can add New Posts using Admin Panel
But now, in Admin Panel we see Postss as option, and Posts object(1) for every entry.
Ok, we will fix that in next tutorial.
No comments:
Post a Comment