码迷,mamicode.com
首页 > 其他好文 > 详细

Django模块笔记【一】

时间:2015-06-25 22:45:01      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

入门笔记翻译整理自:https://docs.djangoproject.com/en/1.8/topics/

*该笔记将对各个模块进行单独介绍

*Model&Database

1. 模型(models)

模型是数据的唯一信息源,它指示了数据的域(fields)和行为(behaviors)。每个模型都对应一个数据库表。

①每个模型都是django.db.models.Model的子类;②模型的每个属性代表了数据库的域;③数据库入口API,参考making queries部分。

比如,

1 from django.db import models
2 
3 class Person(models.Model):
4     first_name = models.CharField(max_length=30)
5     last_name = models.CharField(max_length=30)

对应的数据库表:

1 CREATE TABLE myapp_person (
2     "id" serial NOT NULL PRIMARY KEY,
3     "first_name" varchar(30) NOT NULL,
4     "last_name" varchar(30) NOT NULL
5 );

使用模型:

INSTALLED_APPS = (
    #...
    myapp,
    #...
)

2. 域(Fields)

Django使用Field类来决定:①数据库的列类型(如INTEGER, VARCHAR);②HTML widget(如<input type="text">,<select>)

各种Field的属性不尽相同,但是它们也有一些共同的可选属性。null=True,将空值存储为NULL,默认为False;blank=True,允许空值,默认为False;choices,举例如下:

 1 from django.db import models
 2 
 3 class Person(models.Model):
 4     SHIRT_SIZES = (
 5         (S, Small),
 6         (M, Medium),
 7         (L, Large),
 8     )
 9     name = models.CharField(max_length=60)
10     shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
1 >>> p = Person(name="Fred Flintstone", shirt_size="L")
2 >>> p.save()
3 >>> p.shirt_size
4 L
5 >>> p.get_shirt_size_display()
6 Large

 

default,定义field的默认值;help_text,在表单widget中显示帮助文档;primary_key=True,相应Field为主键,否则会自动生成主键,主键不可更改,比如:

1 from django.db import models
2 
3 class Fruit(models.Model):
4     name = models.CharField(max_length=100, primary_key=True)
1 >>> fruit = Fruit.objects.create(name=Apple)
2 >>> fruit.name = Pear
3 >>> fruit.save()
4 >>> Fruit.objects.values_list(name, flat=True)
5 [Apple, Pear]

 

unique=True,相应Field的值必须为独一无二的。

大多数域可以直接设置域别名(Verbose field names):

1 # verbose name is "person‘s first name"
2 first_name = models.CharField("person‘s first name", max_length=30)
3 # verbose name is "first name"
4 first_name = models.CharField(max_length=30)

 

ForeighKey,ManyToManyField和OneToOneField需要使用verbose_name参数,来设置域别名:

1 poll = models.ForeignKey(Poll, verbose_name="the related poll")
2 sites = models.ManyToManyField(Site, verbose_name="list of sites")
3 place = models.OneToOneField(Place, verbose_name="related place")

 

数据库中常见的三种关系为:多对一,多对多,一对一。

①多对一

1 from django.db import models
2 
3 class Manufacturer(models.Model):
4     # ...
5     pass
6 
7 class Car(models.Model):
8     manufacturer = models.ForeignKey(Manufacturer)
9     # ...

 

②多对多

1 from django.db import models
2 
3 class Topping(models.Model):
4     # ...
5     pass
6 
7 class Pizza(models.Model):
8     # ...
9     toppings = models.ManyToManyField(Topping)

 

或者

 1 from django.db import models
 2 
 3 class Person(models.Model):
 4     name = models.CharField(max_length=128)
 5 
 6     def __str__(self):              # __unicode__ on Python 2
 7         return self.name
 8 
 9 class Group(models.Model):
10     name = models.CharField(max_length=128)
11     members = models.ManyToManyField(Person, through=Membership)
12 
13     def __str__(self):              # __unicode__ on Python 2
14         return self.name
15 
16 class Membership(models.Model):
17     person = models.ForeignKey(Person)
18     group = models.ForeignKey(Group)
19     date_joined = models.DateField()
20     invite_reason = models.CharField(max_length=64)

 

③一对一

 1 from django.db import models
 2 
 3 class Place(models.Model):
 4     name = models.CharField(max_length=50)
 5     address = models.CharField(max_length=80)
 6 
 7     def __str__(self):              # __unicode__ on Python 2
 8         return "%s the place" % self.name
 9 
10 class Restaurant(models.Model):
11     place = models.OneToOneField(Place, primary_key=True)
12     serves_hot_dogs = models.BooleanField(default=False)
13     serves_pizza = models.BooleanField(default=False)
14 
15     def __str__(self):              # __unicode__ on Python 2
16         return "%s the restaurant" % self.place.name

 

3. 文档间模型(Models across files)

需要先导入相应模块:

1 from django.db import models
2 from geography.models import ZipCode
3 
4 class Restaurant(models.Model):
5     # ...
6     zip_code = models.ForeignKey(ZipCode)

 

4. 元选项(Meta options)

meta用于接收一切不是Field的数据。

1 from django.db import models
2 
3 class Ox(models.Model):
4     horn_length = models.IntegerField()
5 
6     class Meta:
7         ordering = ["horn_length"]
8         verbose_name_plural = "oxen"

 

 

 

 

Future work:

model field reference; writing custom model fields

 

Django模块笔记【一】

标签:

原文地址:http://www.cnblogs.com/py-drama/p/4600969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!