Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the understanding of Model Fields in djang

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to understand the understanding of model fields in djang, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Model (model) is the most important knowledge for learning django. The quality of model design directly affects your later development. Model design can only be improved by your own experience. The model accurately and uniquely describes the data, including the important fields and behaviors of the data you store. For example, a student system needs to display information such as students' names, ages, scores and so on, and these data should be saved in the database.

As I said earlier, simple web development is to add, delete, modify and query the database, query data, add data, modify data, and delete data. When you define the model information, the implementation of python manager.py makemigtations will automatically generate a database synchronization script, model fields and database tables generate mapping relationship (this time does not operate the database), after the implementation of python manager.py migrate will automatically execute the database synchronization script to change the database table structure.

Each model is a Python class that inherits django.db.models.Model

Each property of the model class is equivalent to a field of a database. Using these, Django provides an automatic generation to access the database API,django can use ORM to operate the database, even if you are not familiar with SQL syntax, can also be very proficient in operating the database, and even if you later changed the database, the project on the database operation code does not need to change.

Suppose you want to set the character type for each column field of a table, for example, some fields are char type, some are int type, and the function of the model field type in django is to set the data type of the data table mentioned above. Common types are string type CharField, text type TextField, integer type IntegerField, floating point type DecimalField, date type DateTimeField, indicating the foreign key relationship between two tables ForeignKey (one to many) A class has multiple students, many to many is ManyToManyField.

Take the student table (Student) and the class table (Class) as an example. There are multiple students in a class. The detailed field explanation is shown in the following figure:

# CharField is a string type, and the maximum length must be set by max_length. Verbose_name displays the Chinese meaning of the English field in the backstage of django.

# IntegerField Integer

# DecimalField floating point type. Max_digits=5 indicates that the sum of integer parts and decimal places is less than the maximum number of decimal places, null=True indicates that the field can be empty, and blank=True indicates that the data column can be empty in the admin background.

# EmailField this field must conform to the mailbox format

# TextField field is a text type with no limit on its length

# DateTimeField is a date type. Auto_now_add=True the time when the data is created. When the data is updated, the time value will not change.

# auto_now=True the time when the data was created. When the data is updated, the time value will also change.

# ForeignKey one-to-many foreign keys. The data table with the first parameter bit you want to associate with. For example, the table with multiple students in a class belongs to one-to-many, and the foreign key is put into the "many" table. Related_name is an alias for the foreign key and is commonly used in the orm reverse query of django.

The above content is how to understand the understanding of model fields in djang. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report