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

The ultimate framework of perfectionists

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The Django model is related to the database, and the code related to the database is generally written in models.py. Django supports databases such as sqlite3,MySQL,PostgreSQL, which only needs to be configured in settings.py. There is no need to change the code in models.py. Rich API is very convenient to use.

Compared with the traditional three-tier or mvc framework, Model is equivalent to the data processing layer. It is mainly responsible for the interaction with data. When designing applications using the django framework, it should be noted that django defaults to the codefirst model in the orm framework, that is to say, developers only need to focus on the writing of the code, and do not need to pay too much attention to the things at the database level, freeing developers from the database.

Django will generate a database mirror file according to the Model class, and then use the image file to generate the database, and at the same time the file will record changes in the version synchronized with the database, so do not modify the database manually when using django for development, this will result in incorrect version records of the django framework, thus unable to correctly synchronize the data model with the contents of the database. ORM (Objects Relational Mapping) object-relational mapping is referenced in Django, which provides the same calling API for different databases. ORM is a program technology used to convert data between different types of systems in an object-oriented programming language. It can be simply understood as a translator.

Let's take a look at how model creates the database through simple code.

Create projects and APP

Create a project:

Django-admin startproject douban

Create an application:

Under the douban project directory, enter:

Python manage.py startapp book (the name of the app you want to create)

Set up an app called book

In this way, a directory called book will appear in the douban directory of your project

Add our new application (book) to the INSTALLED_APPS in settings.py, that is, tell Django that there is such an application.

Create Model

Enter the models.py in the book directory and enter code similar to the following:

Each of the above class is equivalent to a new table

Django automatically configures each model with a primary key named id

Synchronize database

Enter in shell

Python manage.py makemigrations # establishes the mapping relationship from model to table

Python manage.py migrate # generate tables

After that, you should be able to see things like:

Creating tables...

Creating tablebooks_publisher

Creating tablebooks_author

Creating tablebooks_book_authors

Creating tablebooks_book

Installingcustom SQL...

Installingindexes...

Installed 0object (s) from 0 fixture (s)

This kind of information

The generated table can be seen visually using Navicat

Addition, deletion, modification and query of the database

(insert a new object) insert:

You can operate directly under python-shell.

From book.modelsimport Author

Method 1:

P1 = Author (name='zhangsan',age=30,email='zhangsan@126.com')

P1.save ()

Method 2:

P1 = Author ()

P1. Name='zhangsan'

P1.age = 30

P1. Emailwaters, please zhangsanfang 126.com'

P1.save ()

Method 3:

Author.objects.create (name='zhangsan',age=30,email='zhangsan@126.com')

Method 4:

Author.objects.get_or_create (name='zhangsan',age=30,email='zhangsan@126.com)

The fourth method is a good way to prevent repetition, but the speed is relatively slow, return a tuple, the first is an Author object, the second is True or False, True is returned when new, and False is returned if it already exists.

(query or select objects) select:

You can operate directly under python-shell.

(update object) update:

You can operate directly under python-shell.

(delete object) delete:

You can operate directly under python-shell.

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

Servers

Wechat

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

12
Report