SoFunction
Updated on 2024-11-19

Model class design and presentation examples in Django in detail

django in the design of the data model class is based on ORM object-relational mapping more convenient for data manipulation in the database.

  • object-relational mapping
  • Object-oriented classes and database tables - correspondence, through the operation of the class and object, the number of tables to achieve data operations, do not need to write sql, generated by the ORM framework
  • django implements an ORM framework that creates a bridge between the project and the database
  • The steps to define a model for a django database are as follows:
python  makemigrations
python  migrate
  • Write model classes in the application, inheritance classes
  • In the model class, define the attributes to generate the corresponding database table fields
  • Property Name = models.Field Type
  • Field types - need to be re-migrated once the type is changed

AutoField: auto-growth, usually doesn't need to be specified, django automatically creates an auto-growth attribute named id.
CharField: string, must be specified: max_length max_number_of_characters
TextFiled: large text field, typically over 4000 characters
IntegerField: Integer
BooleanField: Boolean, supports Null, True
NullBooleanField: Supports Null, True, False.
DateTimeField: DateTime
FileField: Upload file field
ImageField: Inherited from FileFiled, checks the uploaded content to make sure it is a valid image.
ForeignKey: Foreign key to create a one-to-many relationship

from  import models

# Create your models here.

class Subject():
 """Discipline Modeling Class"""
 nu = (primary_key=True)
 name = (max_length=20,verbose_name = 'Subject name')
 intro = (max_length=1000,verbose_name = 'Introduction to the discipline')

 class Meta:
  db_table = 'tb_subject'
  verbose_name = 'Discipline'
  verbose_name_plural = verbose_name

 def __str__(self):
  """Define the display information of the data object"""
  return 

Perform a database creation migration in the pycharm terminal

python makemigrations

python migrate

A .py file is created in the migrations file

We can see the data model classes we've created through the admin backend that comes with django.

Aadmin

To use it, create a username and password

In the pycharm terminal, enter the command

python createsuperuser

Follow the prompts to enter your username, email, and password.

And in registering the model class and displaying the list of data that should be there.

Then open the django project and type http://127.0.0.1 :8000/admin/

User name and password fill in

Once you log in you will see the data model created

Click add to add some data

There will be modeling classes that you write

This is because the SubjectAdmin class was just inherited to ask for information about the discipline to be displayed.

We can now map the model data to the view and display it to the html page.

Use Subject in the file to get the collection of data to the

Create a file in the templates folder

Inserting data into an html page using a for loop in a django template

Start django and open the local URL

We can add photos to the page to make it look better.

Add the folder where the photos will be placed in a directory on the same level as the more directory

And specify the path to the static folder in settings

After changing the html page

Adding a photo path

Then start django and click on local connection

Next, you can add the model database again, and when you click on a subject you can show those teachers and their descriptions. Many-to-one or one-to-many data is realized.

Create the teacher's model class in

Registered teachers get modeling classes in

Go to the admin background and add some data of the teacher

Now click on the subject and go to the teacher's details page, that of writing html files and modifying the

Create a file in the templatels folder.

In Writing.

Then click on the subject to get teacher data

django's simple project about the model's presentation design is all done!!!!

summarize

to this article on the Django model class design and display examples to this article, more related django model class design and display content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!