Django数据库配置【models】

来源:互联网 发布:linux虚拟机网络不通 编辑:程序博客网 时间:2024/06/07 03:32

可用的字段

1、models.AutoField  自增列 = int(11)  如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设置为主键 primary_key=True2、models.CharField  字符串字段  必须 max_length 参数3、models.BooleanField  布尔类型=tinyint(1)  不能为空,Blank=True4、models.ComaSeparatedIntegerField  用逗号分割的数字=varchar  继承CharField,所以必须 max_lenght 参数5、models.DateField  日期类型 date;对于参数,auto_now = True 则每次更新都会更新这个时间;auto_now_add 则只是第一次创建添加,之后的更新不再改变。6、models.DateTimeField  日期类型 datetime                         同DateField的参数7、models.Decimal  十进制小数类型 = decimal                   必须指定整数位max_digits和小数位decimal_places8、models.EmailField  字符串类型(正则表达式邮箱) =varchar                      对字符串进行正则表达式9、models.FloatField  浮点类型 = double10、models.IntegerField  整形11、models.BigIntegerField  长整形  integer_field_ranges = {    'SmallIntegerField': (-32768, 32767),    'IntegerField': (-2147483648, 2147483647),    'BigIntegerField': (-9223372036854775808, 9223372036854775807),    'PositiveSmallIntegerField': (0, 32767),    'PositiveIntegerField': (0, 2147483647),  }12、models.IPAddressField  字符串类型(ip4正则表达式)13、models.GenericIPAddressField  字符串类型(ip4和ip6是可选的)                                  参数protocol可以是:both、ipv4、ipv6                                  验证时,会根据设置报错14、models.NullBooleanField  允许为空的布尔类型15、models.PositiveIntegerFiel  正Integer16、models.PositiveSmallIntegerField  正smallInteger17、models.SlugField  减号、下划线、字母、数字18、models.SmallIntegerField  数字  数据库中的字段有:tinyint、smallint、int、bigint19、models.TextField  字符串=longtext20、models.TimeField  时间 HH:MM[:ss[.uuuuuu]]21、models.URLField  字符串,地址正则表达式22、models.BinaryField  二进制23、models.ImageField   图片24、models.FilePathField 文件

字段相关参数

1、null=True  数据库中字段是否可以为空2、blank=True  django的 Admin 中添加数据时是否可允许空值3、primary_key = False  主键,对AutoField设置主键后,就会代替原来的自增 id 列4、auto_now 和 auto_now_add  auto_now   自动创建---无论添加或修改,都是当前操作的时间  auto_now_add  自动创建---永远是创建时的时间5、choicesGENDER_CHOICE = (        (u'M', u'Male'),        (u'F', u'Female'),    )gender = models.CharField(max_length=2,choices = GENDER_CHOICE)6、max_length7、default  默认值8、verbose_name  Admin中字段的显示名称9、name|db_column  数据库中的字段名称10、unique=True  不允许重复11、db_index = True  数据库索引12、editable=True  在Admin里是否可编辑13、error_messages=None  错误提示14、auto_created=False  自动创建15、help_text  在Admin中提示帮助信息16、validators=[]17、upload-to

查询方法

  • blog的enrty的headline属性值是“Lennon”,或者entry的发表时间是2008(两个条件至少满足一个,也可以同时满足)
    Blog.objects.filter(entry__headline__contains=’Lennon’).filter(entry__pub_date__year=2008)
  • 两个条件是headline 包含Lennon 和发表时间是2008 (同一个entry 满足两个条件)
    Blog.objects.filter(entry__headline__contains=’Lennon’,entry__pub_date__year=2008)
  • 不是排除同时满足两个条件的Entry
    Blog.objects.exclude(entry__headline__contains=’Lennon’,entry__pub_date__year=2008,)
  • 排除同时满足两个条件的Entry
    Blog.objects.exclude(entry=Entry.objects.filter(headline__contains=’Lennon’,pub_date__year=2008,),)
    转自http://www.cnblogs.com/pycode/p/db-middleware.html
0 0
原创粉丝点击