1.字段类型与方法(Field type & function):
2.字段方法参数:
choice示例:
from django.dbimport models
classStudent(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
(FRESHMAN, 'Freshman'),#第一个参数是真正的model参数,#第二个参数则是方便人们理解阅读
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
)
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
db_column = "学年"
)
def is_upperclass(self):
return self.year_in_school in(self.JUNIOR, self.SENIOR)
3,关系型的字段
除了一些字段的类型的Field类,django还提供了一些关系型的fields,比如外键、一对多,多对多等,下面将做一个简单的解释:
3.1一对多
ForeignKey:外键。语法是ForeignKey(othermodel,on_delete)
othermodel表示的是其他模型,注意,如果这个othermodel模型在本模型之后定义的,则必须使用字符串表示。
on_delete表示删除的时候怎么处理,可以取如下值:
CASCADE:级联删除,如果删除一,相关联的那个多也会删除
PROTECT:保护类型。如果删除,将会抛出一个ProtectedError错误
SET_NULL:如果删除了本条数据,外键的那条数据将会设置为null,这个只有在外键null为True的情况下才可以使用
SET_DEFAULT:如果删除了本条数据,外键那条数据将会置为默认值,这个只有在外键那个字段设置了default参数才可以使用。
操作:
classPublisher(models.Model):
name =models.CharField(max_length=30)
address =models.CharField(max_length=50)
# city =models.CharField(max_length=60)
#state_province =models.CharField(max_length=30)
# country =models.CharField(max_length=50)
# website =models.URLField()
def __unicode__(self):
return self.name
classAuthor(models.Model):
name =models.CharField(max_length=30)
age = models.IntegerField()
email =models.EmailField()
def __unicode__(self):
return self.name
classBook(models.Model):
title =models.CharField(max_length=100)
authors =models.ManyToManyField(Author)
publisher =models.ForeignKey(Publisher)
# publication_date =models.DateField()
def __unicode__(self):
return self.title
进入python manage.py shell:
>>>p = Publisher(name="清华大学出版社",address="北京")
>>>p.save()
>>> b= Book(title="python web指南")
>>>b.publisher = p
>>>b.save()
3.2多对多
ManyToManyField:语法是ManyToManyField(othermodel)
othermodel表示的是其他模型,如果othermodel这个模型在本模型之后定义的,则必须使用字符串(’othermodel’)
>>>a = Author(name="张三",age=40)
>>>a.save()
>>>b = Book(title="python web指南")
>>>b.author.add(a)
>>>b.save()
4.自定义字段
books/listfield.py
from django.db import models
import ast
class ListField(models.TextField):
#__metaclass__ = models.SubfieldBase
description= "Stores a python list"
def__init__(self, *args, **kwargs):
super(ListField, self).__init__(*args, **kwargs)
defto_python(self, value):
if notvalue:
value = []
ifisinstance(value, list):
return value
returnast.literal_eval(value)
defget_prep_value(self, value):
if valueis None:
return value
returnunicode(value) # use str(value) in Python 3
defvalue_to_string(self, obj):
value =self._get_val_from_obj(obj)
returnself.get_db_prep_value(value)
注意:
to_python函数用于转化数据库中的字符到Python的变量,get_prep_value用于将Python变量处理后保存到数据库,
value_to_string用于print
books/models.py:
from books.listfield import ListField
...
class Author(models.Model):
name =models.CharField(max_length=30)
age =models.IntegerField()
email =models.EmailField(null=True,blank=True,unique=True)
area =ListField(null=True)
python manage.py shell:
>>>a =Author(name="lisi",age=30,email="lisi@1226.com")
>>>a.area =["python","java","c++"]
>>>print a.area