python例子-Django之模型(Model_数据库)

    Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.py中配置即可,不用更改models.py中的代码

一、打开app下的models.py 并编辑你的model:

from django.db import models

# Create your models here.
class Person(models.Model):
        name = models.CharField(max_length=30)
        age = models.IntegerField()

        def __unicode__(self):
                return self.name

保存退出

二、改变当前目录到当前项目下:

  执行 python manage.py syncdb #进入 manage.py 所在的那个文件夹下输入这个命令

 
  注意Django 1.7 及以上的版本需要用以下命令
  python manage.py makemigrations
  python manage.py migrate
[cos@localhost zqxt_tmpl]$ python manage.py syncdb
/usr/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
[cos@localhost zqxt_tmpl]$ python manage.py makemigrations
Migrations for 'learn':
  0001_initial.py:
    - Create model Person
[cos@localhost zqxt_tmpl]$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, sessions, auth, learn
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE

  并执行 python manage.py shell 来进行当前项目环境.

>>> from learn.models import Person    #这里注意是我们的 app.models import Person ,Person 是我们定义的一个类.app 是我们项目下的一个app应用.
>>> Person.objects.create(name='wanghao',age=21) #这里是 <Person: wanghao> >>> Person.objects.get(name='wangchao') <Person: wangchao> >>> people = Person.objects.all() >>> print people [<Person: wangchao>, <Person: wanghao>] >>> people = Person.objects.all()[:1] >>> people [<Person: wangchao>] >>> people = Person.objects.all()[:10] >>> people [<Person: wangchao>, <Person: wanghao>]

三、新建一个对象的方法有以下几种:

  1. Person.objects.create(name=name,age=age)

  2. p = Person(name="WZ", age=23)

    p.save()

  3. p = Person(name="TWZ")

    p.age = 23

    p.save()

  4. Person.objects.get_or_create(name="WZT", age=23)

    这种方法是防止重复很好的方法,但是速度要相对慢些,返回一个元组,第一个为Person对象,第二个为True或False, 新建时返回的是True, 已经存在时返回False.

四、获取对象有以下方法:

  1. Person.objects.all()

  2. Person.objects.all()[:10] 切片操作,获取10个人,不支持负索引,切片可以节约内存

  3. Person.objects.get(name=name)

    get是用来获取一个对象的,如果需要获取满足条件的一些人,就要用到filter

  4. Person.objects.filter(name="abc") # 等于Person.objects.filter(name__exact="abc") 名称严格等于 "abc" 的人

  5. Person.objects.filter(name__iexact="abc") # 名称为 abc 但是不区分大小写,可以找到 ABC, Abc, aBC,这些都符合条件

  6. Person.objects.filter(name__contains="abc") # 名称中包含 "abc"的人

  7. Person.objects.filter(name__icontains="abc") #名称中包含 "abc",且abc不区分大小写

  8. Person.objects.filter(name__regex="^abc") # 正则表达式查询

  9. Person.objects.filter(name__iregex="^abc")# 正则表达式不区分大小写

    filter是找出满足条件的,当然也有排除符合某条件的

  10. Person.objects.exclude(name__contains="WZ") # 排除包含 WZ 的Person对象

  11. Person.objects.filter(name__contains="abc").exclude(age=23) # 找出名称含有abc, 但是排除年龄是23岁的

Django models 官方教程: https://docs.djangoproject.com/en/dev/topics/db/models/

Fields相关官方文档:https://docs.djangoproject.com/en/dev/ref/models/fields/

本文参考:http://www.ziqiangxuetang.com/django/django-models.html

posted @ 2015-10-23 20:48  超超xc  Views(862)  Comments(0Edit  收藏  举报
I suppose,were childrenonec.