Home > Database > Mysql Tutorial > body text

Django中的模型与数据库(Models and database)

WBOY
Release: 2016-06-07 15:15:57
Original
1428 people have browsed it

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>
Copy after login

这里我就有一个疑问,到底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>
Copy after login

接下来我们再来说说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>
Copy after login

Field option

<code>每一个Field都有一个特定参数,例如CharField,max_length
null  如果是真,jango将会存储空值null进数据库 默认为false
blank 如果为真,field将会允许空格的输入, 默认为false
choices 一个二位元组,如果field中使用choices 则在前端展示的HTML组件就是<select>
</select></code>
Copy after login

举个例子

<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>
Copy after login

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>
Copy after login

当你执行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>
Copy after login

字段查询

__id

<code>被指定的查询字段名字必须是模型field名字相对应,除非外键的情况

Entry.objects.filter(blog_id=<span>4</span>)

这时候返回并不是Entry中id=<span>4</span>的数据行,而是id对应主键算在的数据行</code>
Copy after login

__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>
Copy after login

__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>
Copy after login

__contains

<code>Entry.objects.get(headline__contains=’Lennon’)

模糊搜索,翻译成sql

SELECT ... WHERE headline LIKE ’%Lennon%’;</code>
Copy after login

__ icontains

<code>Entry.objects.get(headline__icontains=’Lennon’)

sql:

SELECT ... WHERE headline ILIKE ’%Lennon%’;</code>
Copy after login

__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>
Copy after login

__gt 大于

<code>Entry.objects.filter(id__gt=<span>4</span>)
sql:
SELECT … WHERE id > <span>4</span>
__gte 大于等于
__lt 小于
__lte 小于等于</code>
Copy after login

__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>
Copy after login

__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>
Copy after login

__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>
Copy after login

__isnull

<code>Entry.objects.filter(pub_date__isnull=<span>True</span>)
sql:
SELECT ... WHERE pub_date IS NULL;</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template