Django中的模型与数据库(Models and database)
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>

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Earlier this month, researchers from MIT and other institutions proposed a very promising alternative to MLP - KAN. KAN outperforms MLP in terms of accuracy and interpretability. And it can outperform MLP running with a larger number of parameters with a very small number of parameters. For example, the authors stated that they used KAN to reproduce DeepMind's results with a smaller network and a higher degree of automation. Specifically, DeepMind's MLP has about 300,000 parameters, while KAN only has about 200 parameters. KAN has a strong mathematical foundation like MLP. MLP is based on the universal approximation theorem, while KAN is based on the Kolmogorov-Arnold representation theorem. As shown in the figure below, KAN has

In order to align large language models (LLMs) with human values and intentions, it is critical to learn human feedback to ensure that they are useful, honest, and harmless. In terms of aligning LLM, an effective method is reinforcement learning based on human feedback (RLHF). Although the results of the RLHF method are excellent, there are some optimization challenges involved. This involves training a reward model and then optimizing a policy model to maximize that reward. Recently, some researchers have explored simpler offline algorithms, one of which is direct preference optimization (DPO). DPO learns the policy model directly based on preference data by parameterizing the reward function in RLHF, thus eliminating the need for an explicit reward model. This method is simple and stable

At the forefront of software technology, UIUC Zhang Lingming's group, together with researchers from the BigCode organization, recently announced the StarCoder2-15B-Instruct large code model. This innovative achievement achieved a significant breakthrough in code generation tasks, successfully surpassing CodeLlama-70B-Instruct and reaching the top of the code generation performance list. The unique feature of StarCoder2-15B-Instruct is its pure self-alignment strategy. The entire training process is open, transparent, and completely autonomous and controllable. The model generates thousands of instructions via StarCoder2-15B in response to fine-tuning the StarCoder-15B base model without relying on expensive manual annotation.

1. Introduction Over the past few years, YOLOs have become the dominant paradigm in the field of real-time object detection due to its effective balance between computational cost and detection performance. Researchers have explored YOLO's architectural design, optimization goals, data expansion strategies, etc., and have made significant progress. At the same time, relying on non-maximum suppression (NMS) for post-processing hinders end-to-end deployment of YOLO and adversely affects inference latency. In YOLOs, the design of various components lacks comprehensive and thorough inspection, resulting in significant computational redundancy and limiting the capabilities of the model. It offers suboptimal efficiency, and relatively large potential for performance improvement. In this work, the goal is to further improve the performance efficiency boundary of YOLO from both post-processing and model architecture. to this end

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

The benchmark YOLO series of target detection systems has once again received a major upgrade. Since the release of YOLOv9 in February this year, the baton of the YOLO (YouOnlyLookOnce) series has been passed to the hands of researchers at Tsinghua University. Last weekend, the news of the launch of YOLOv10 attracted the attention of the AI community. It is considered a breakthrough framework in the field of computer vision and is known for its real-time end-to-end object detection capabilities, continuing the legacy of the YOLO series by providing a powerful solution that combines efficiency and accuracy. Paper address: https://arxiv.org/pdf/2405.14458 Project address: https://github.com/THU-MIG/yo

French AI unicorn MistralAI, which is targeting OpenAI, has made a new move: Codestral, the first large code model, was born. As an open generative AI model designed specifically for code generation tasks, Codestral helps developers write and interact with code by sharing instructions and completion API endpoints. Codestral's proficiency in coding and English allows software developers to design advanced AI applications. The parameter size of Codestral is 22B, it complies with the new MistralAINon-ProductionLicense, and can be used for research and testing purposes, but commercial use is prohibited. Currently, the model is available for download on HuggingFace. download link

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())
