Django中的模型与数据库(Models and database) 对于数据库大家都不陌生,但是Models该怎么理解,官方文档中有这么一句话: A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of thedata yo
Django中的模型与数据库(Models and database)
对于数据库大家都不陌生,但是Models该怎么理解,官方文档中有这么一句话:
A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of thedata you’re storing. Generally, each model maps to a single database table.
下文暂且称Models为“模型”(个人叫法)
那么,模型具备以下三个特征:
每一个模型都是一个python子类。继承django.db.models.Model模型的每一个属性代表一个数据库字段除了上述之外,jango还提供了自动生成数据库访问的API为了方便理解我们举个例子下面这个例子我们建立了一个Person的模型,且有两个字段)first_name,last_name
<code><span>from</span> django.db <span>import</span> models <span><span>class</span> <span>Person</span><span>(models.Model)</span>:</span> first_name = models.CharField(max_length=<span>30</span>) last_name = models.CharField(max_length=<span>30</span>) first_name和last_name是Person模型的Field,每一个Field都是一个指定的类的属性,每一个属性映射到数据库的没一列</code>
这里我就有一个疑问,到底Field能不能称为字段?
<code>上面的Person模型将会对应一张这样的数据库表: CREATE TABLE myapp_person ( <span>"id"</span> serial NOT NULL PRIMARY KEY, <span>"first_name"</span> varchar(<span>30</span>) NOT NULL, <span>"last_name"</span> varchar(<span>30</span>) NOT NULL ); 下面我们聊聊使用,一旦你声明了一个models,你需要去告诉jango你将会去使用该models,在你的settings里把你的 应用名加到INSTALLED_APPS中 INSTALLED_APPS = ( <span>#...</span> ’myapp’, <span>#...</span> ) 当你在INSTALLED_APPS添加新应用后,需要执行manage.py migrate </code>
接下来我们再来说说Fields
<code>它是模型的重要的一部分,它定义了数据库表的字段 <span>from</span> django.db <span>import</span> models <span><span>class</span> <span>Musician</span><span>(models.Model)</span>:</span> first_name = models.CharField(max_length=<span>50</span>) last_name = models.CharField(max_length=<span>50</span>) instrument = models.CharField(max_length=<span>100</span>) <span><span>class</span> <span>Album</span><span>(models.Model)</span>:</span> artist = models.ForeignKey(Musician) name = models.CharField(max_length=<span>100</span>) release_date = models.DateField() num_stars = models.IntegerField() 模型中每一个field是一个Field类实例,jango通过Field类类型去确定一下几个点。 <span>1.</span>数据库字段的类型 <span>2.</span>使用默认的HTML部件但渲染表单的field时(e.g. <input type="“text”">,<select>)</select></code>
Field option
<code>每一个Field都有一个特定参数,例如CharField,max_length null 如果是真,jango将会存储空值null进数据库 默认为false blank 如果为真,field将会允许空格的输入, 默认为false choices 一个二位元组,如果field中使用choices 则在前端展示的HTML组件就是<select> </select></code>
举个例子
<code> YEAR_IN_SCHOOL_CHOICES = ( (’FR’, ’Freshman’), (’SO’, ’Sophomore’), (’JR’, ’Junior’), (’SR’, ’Senior’), (’GR’, ’Graduate’), ) <span>from</span> django.db <span>import</span> models <span><span>class</span> <span>Person</span><span>(models.Model)</span>:</span> SHIRT_SIZES = ( (’S’, ’Small’), (’M’, ’Medium’), (’L’, ’Large’), ) name = models.CharField(max_length=<span>60</span>) shirt_size = models.CharField(max_length=<span>1</span>,choices=SHIRT_SIZES) <span>>>> </span>p = Person(name=<span>"Fred Flintstone"</span>, shirt_size=<span>"L"</span>) <span>>>> </span>p.save() <span>>>> </span>p.shirt_size u’L’ <span>>>> </span>p.get_shirt_size_display() u’Large’</code>
save()
这个在后台执行了一个插入sql。但是并没有真正的导数据库知道用户执行了save(),save()没有返回值,但是save()支持参数
<code>Model.save ([force_insert=<span>False</span>, force_update=<span>False</span>, using=DEFAULT_DB_ALIAS, update_fields=<span>None</span> ] )</code>
当你执行save()操作,jango执行以下步鄹
1:触发pre-save事件,这个信号,任何一个函数都可以监听这个事件
2:一些有特殊的字段类型的字段做处理,例如dateField和auto_now=True这时候得到的值是jango生成的时间,这里要注意的是,数据库的时间可能跟服务器的不一样,所以这里要注意时间同步。
3:为插入准备数据,每一个字段提供一个处理后的值
4:为插入准备sql,这里我理解为拼sql
5:发给数据库执行sql
all()
<code>all_entries = Entry.objects.all() 查看所有的内容 filter(**kwargs) 我们现在看下通过过滤器(filters)获取具体的值 Entry.objects.filter(pub_date__year=<span>2006</span>) exclude(**kwargs) Entry.objects.filter( <span>... </span>headline__startswith=’What’ <span>... </span>).exclude( <span>... </span>pub_date__gte=datetime.date.today() <span>... </span>).filter( <span>... </span>pub_date__gte=datetime(<span>2005</span>, <span>1</span>, <span>30</span>) … ) 返回除去与查找条件相符的数据 get() 如果想要返回指定的一个数据 one_enty = Entry.objects.get(pk=<span>1</span>) </code>
字段查询
__id
<code>被指定的查询字段名字必须是模型field名字相对应,除非外键的情况 Entry.objects.filter(blog_id=<span>4</span>) 这时候返回并不是Entry中id=<span>4</span>的数据行,而是id对应主键算在的数据行</code>
__exact
<code>最普通的情况(默认添加) Entry.objects.get(headline__exact=<span>"Man bites dog"</span>) 翻译成sql就为 SELECT ... WHERE headline = ’Man bites dog’; Blog.objects.get(id__exact=<span>14</span>) <span># 明确的形式</span> Blog.objects.get(id=<span>14</span>) <span># __exact 默认添加</span></code>
__iexact
<code>Blog.objects.get(name__iexact=<span>"beatles blog"</span>) 结果可能是 <span>"Beatles Blog"</span>, <span>"beatles blog"</span>, <span>or</span> <span>"BeAtlES blOG"</span>. 不区分大小写</code>
__contains
<code>Entry.objects.get(headline__contains=’Lennon’) 模糊搜索,翻译成sql SELECT ... WHERE headline LIKE ’%Lennon%’;</code>
__ icontains
<code>Entry.objects.get(headline__icontains=’Lennon’) sql: SELECT ... WHERE headline ILIKE ’%Lennon%’;</code>
__in
<code>Entry.objects.filter(id__in=[<span>1</span>,<span>3</span>,<span>4</span>] sql: SELECT … WHERE id IN (<span>1</span>,<span>3</span>,<span>4</span>); 这种也可以用复合sql的形式表示 inner_qs = Blog.objects.filter(name__contains=’Cheddar’) entries = Entry.objects.filter(blog__in=inner_qs) sql: SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE ’%Cheddar%’)</code>
__gt 大于
<code>Entry.objects.filter(id__gt=<span>4</span>) sql: SELECT … WHERE id > <span>4</span> __gte 大于等于 __lt 小于 __lte 小于等于</code>
__range
<code><span>import</span> datetime start_date = datetime.date(<span>2005</span>, <span>1</span>, <span>1</span>) end_date = datetime.date(<span>2005</span>, <span>3</span>, <span>31</span>) Entry.objects.filter(pub_date__range=(start_date, end_date)) sql: SELECT ... WHERE pub_date BETWEEN ’<span>2005</span>-<span>01</span>-<span>01</span>’ <span>and</span> ’<span>2005</span>-<span>03</span>-<span>31</span>’;</code>
__year
<code>Entry.objects.filter(pub_date_year=<span>2005</span>) sql: SELECT … WHERE pub_date BETWEEN ‘<span>2005</span>-<span>01</span>-<span>01</span>’ <span>and</span> ‘<span>2005</span>-<span>12</span>-<span>31</span>’;</code>
__month
__day
__hour
__minute
<code>Entry.objects.filter(pub_date__month=<span>12</span>) sql: SELECT ... WHERE EXTRACT(’month’ FROM pub_date) = ’<span>12</span>’;</code>
__isnull
<code>Entry.objects.filter(pub_date__isnull=<span>True</span>) sql: SELECT ... WHERE pub_date IS NULL;</code>

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor