> 데이터 베이스 > MySQL 튜토리얼 > Django中的模型与数据库(Models and database)

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

WBOY
풀어 주다: 2016-06-07 15:15:57
원래의
1449명이 탐색했습니다.

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>
로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿