当前位置: 首页 > news >正文

购买网站app制作专门做网站开发的公司

购买网站app制作,专门做网站开发的公司,永州静默管理,物流网站后台本章内容 Django model Model 基础配置 django默认支持sqlite#xff0c;mysql, oracle,postgresql数据库。 1 sqlite django默认使用sqlite的数据库#xff0c;默认自带sqlite的数据库驱动 引擎名称#xff1a;django.db.backends.sqlite3 2mysql …本章内容 Django model Model 基础配置 django默认支持sqlitemysql, oracle,postgresql数据库。 1 sqlite django默认使用sqlite的数据库默认自带sqlite的数据库驱动 引擎名称django.db.backends.sqlite3 2mysql 引擎名称django.db.backends.mysql 1、配置文件中sqlite DATABASES {default: {ENGINE: django.db.backends.sqlite3,NAME: os.path.join(BASE_DIR, db.sqlite3),} }2、配置文件中mysql DATABASES {default: {ENGINE: django.db.backends.mysql,NAME:数据库名,USER: root,PASSWORD: xxx,HOST: ,PORT: ,} }注由于Django内部连接MySQL时使用的是MySQLdb模块而python3中还无此模块所以需要使用pymysql来代替 如下设置放置的与project同名的配置的 init.py文件中 import pymysql pymysql.install_as_MySQLdb() Model 创建表结构 创建的时候必须放置models文件中其他文件中均不可创建 一、基本结构 from django.db import models class Content(models.Model):title models.CharField(max_length150)body models.TextField()timestamp models.DateTimeField()Contentt里用了CharFieldTextField, TextField域。难道就只有这三种不可能 如下我列出了其它的Field并表明了它们的继承关系 Field |--AutoField |--BooleanField |--CharField | |--EmailField | |--SlugField | --URLField |--DateField | --DateTimeField |--DecimalField |--FilePathField |--FloatField |--IntegerField | |--BigIntegerField | |--PositiveIntegerField | --PositiveSmallIntegerField |--IPAddressField |--GenericIPAddressField |--NullBooleanField |--TextField |--TimeField --BinaryField字段注释 1、models.AutoField  自增列 int(11)如果没有的话默认会生成一个名称为 id 的列如果要显示的自定义一个自增列必须将给列设置为主键 primary_keyTrue。 2、models.CharField  字符串字段必须 max_length 参数 3、models.BooleanField  布尔类型tinyint(1)不能为空BlankTrue 4、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_places 8、models.EmailField  字符串类型正则表达式邮箱 varchar对字符串进行正则表达式 9、models.FloatField  浮点类型 double 10、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  正Integer 16、models.PositiveSmallIntegerField  正smallInteger 17、models.SlugField  减号、下划线、字母、数字 18、models.SmallIntegerField  数字数据库中的字段有tinyint、smallint、int、bigint 19、models.TextField  字符串longtext 20、models.TimeField  时间 HH:MM[:ss[.uuuuuu]] 21、models.URLField  字符串地址正则表达式 22、models.BinaryField  二进制 23、models.ImageField 图片 24、models.FilePathField 文件Field类在构造的时候可以指定以下参数 1、nullTrue数据库中字段是否可以为空 2、blankTruedjango的 Admin 中添加数据时是否可允许空值 3、primary_key False主键对AutoField设置主键后就会代替原来的自增 id 列 4、auto_now 和 auto_now_addauto_now 自动创建---无论添加或修改都是当前操作的时间auto_now_add 自动创建---永远是创建时的时间 5、choices GENDER_CHOICE ((uM, uMale),(uF, uFemale),) gender models.CharField(max_length2,choices GENDER_CHOICE) 6、max_length 7、default  默认值 8、verbose_name  Admin中字段的显示名称 9、name|db_column  数据库中的字段名称 10、uniqueTrue  不允许重复 11、db_index True  数据库索引 12、editableTrue  在Admin里是否可编辑 13、error_messagesNone  错误提示 14、auto_createdFalse  自动创建 15、help_text  在Admin中提示帮助信息 16、validators[] 17、upload-to二、连表结构 一对多models.ForeignKey(其他表)多对多models.ManyToManyField(其他表)一对一models.OneToOneField(其他表) 应用场景 1、一对一 当一张表中创建一行数据时有一个单选的下拉框可以被重复选择 例创建用户信息时候需要选择一个用户类型【普通用户】【金牌用户】【铂金用户】等。2、多对多 在某表中创建一行数据是有一个可以多选的下拉框 例创建用户信息需要为用户指定多个爱好3、一对一 在某表中创建一行数据时有一个单选的下拉框下拉框中的内容被用过一次就消失了 例原有含10列数据的一张表保存相关信息经过一段时间之后10列无法满足需求需要为原来的表再添加5列数据Model 操作 一、基本操作 1、增 # 方式一 models.Tb1.objects.create(c1xx, c2oo) 增加一条数据可以接受字典类型数据 **kwargs# 方式二 obj models.Tb1(c1xx, c2oo) obj.save()2、删 models.Tb1.objects.filter(name seven).delete() # 删除指定条件的数据3、改 # 方式一 models.Tb1.objects.filter(nameseven).update(gender0) # 将指定条件的数据更新均支持 **kwargs# 方式二 obj models.Tb1.objects.get(id1) obj.c1 111 obj.save() # 修改单条数据4、查 models.Tb1.objects.get(id123) # 获取单条数据不存在则报错不建议 models.Tb1.objects.all() # 获取全部 models.Tb1.objects.filter(nameseven) # 获取指定条件的数据二、进阶操作 利用双下划线将字段和对应的操作连接起来 # 获取个数## models.Tb1.objects.filter(nameseven).count()# 大于小于## models.Tb1.objects.filter(id__gt1) # 获取id大于1的值# models.Tb1.objects.filter(id__lt10) # 获取id小于10的值# models.Tb1.objects.filter(id__lt10, id__gt1) # 获取id大于1 且 小于10的值# in## models.Tb1.objects.filter(id__in[11, 22, 33]) # 获取id等于11、22、33的数据# models.Tb1.objects.exclude(id__in[11, 22, 33]) # not in# contains## models.Tb1.objects.filter(name__containsven)# models.Tb1.objects.filter(name__icontainsven) # icontains大小写不敏感# models.Tb1.objects.exclude(name__icontainsven)# range## models.Tb1.objects.filter(id__range[1, 2]) # 范围bettwen and# 其他类似## startswithistartswith, endswith, iendswith,# order by## models.Tb1.objects.filter(nameseven).order_by(id) # asc# models.Tb1.objects.filter(nameseven).order_by(-id) # desc# limit 、offset## models.Tb1.objects.all()[10:20]# group byfrom django.db.models import Count, Min, Max, Sum# models.Tb1.objects.filter(c11).values(id).annotate(cCount(num))# SELECT app01_tb1.id, COUNT(app01_tb1.num) AS c FROM app01_tb1 WHERE app01_tb1.c1 1 GROUP BY app01_tb1.idModel 操作案例 一、一对多案例 1、表结构如下 from django.db import modelsclass Usertype(models.Model):uid models.AutoField(primary_keyTrue)caption models.CharField(max_length32)class Userinfo(models.Model):user models.CharField(max_length32)email models.EmailField(max_length32)pwd models.CharField(max_length32)user_type models.ForeignKey(Usertype)打开终端进入project目录下输入命令生成表结构 python3 manage.py makemigrations python3 manage.py migrate2、创建数据 def index(request):# 三种创建数据方法obj models.Usertype(caption 管理员 )obj.save()models.Usertype.objects.create(caption普通用户)obj {caption:Guest}models.Usertype.objects.create(**obj)# 建议使用这一种方法userinfo_dict {user:张岩林,email:123qq.com,pwd:123,user_type_id:1,}userinfo_dicts {user:aylin,email:124qq.com,pwd:1333,user_type:models.Usertype.objects.get(uid3),}models.Userinfo.objects.create(**userinfo_dict)models.Userinfo.objects.create(**userinfo_dicts)return HttpResponse(OK)3、单表查询 def index(request):# 单表查询,查询结果是queryset对象ret models.Usertype.objects.all()print(ret) # 结果QuerySet [Usertype: Usertype object, Usertype: Usertype object, Usertype: Usertype object]for item in ret:print(item.caption) # 取值管理员,普通用户guest# 单独只取一列 vlauesret models.Usertype.objects.all().values(caption)print(ret)for i in ret:print(i,type(i)) # {caption: 管理员} class dict# 可以通过变量取到关联表的对象后面加入.caption取另一个表的内容ret models.Userinfo.objects.all()print(ret,type(ret))for item in ret:print(item.user,item.user_type.caption) # item.user_type取到的是usertype的对象所以可以取captionreturn HttpResponse(OK)4、连表正向查找双下划线 def index(request):# 连表查询之双下划线ret models.Userinfo.objects.all().values(user,user_type__caption)print(ret)# 过滤所有用户类型是管理员的用户(连表查询)ret models.Userinfo.objects.filter(user_type__caption管理员).values(user, user_type__caption)print(ret)return HttpResponse(OK)5、连表反向查找双下划线 def index(request):# 过滤取出第一个是管理员的哪一行数据obj models.Usertype.objects.filter(caption管理员).first()print(obj.uid)print(obj.caption)print(obj.userinfo_set.all())obj models.Usertype.objects.filter(userinfo__user张岩林).values(userinfo__user)print(obj)return HttpResponse(OK)二、多对多操作 1、表结构如下 # 1、传统多对多创建表 class HostToGroup(models.Model):hgid models.AutoField(primary_keyTrue)host_id models.ForeignKey(Host)group_id models.ForeignKey(Group)class Host(models.Model):hid models.AutoField(primary_keyTrue)hostname models.CharField(max_length32)ip models.CharField(max_length32)class Group(models.Model):gid models.AutoField(primary_keyTrue)name models.CharField(max_length16)# 2、django给创建第三章关系表同时可以通过group表里的h2g来给第三张表进行操作class Host(models.Model):hid models.AutoField(primary_keyTrue)hostname models.CharField(max_length32)ip models.CharField(max_length32)# h2g models.ManyToManyField(Group) class Group(models.Model):gid models.AutoField(primary_keyTrue)name models.CharField(max_length16)h2g models.ManyToManyField(Host)# 3、自己写第三张表的时候many要加参数class Host(models.Model):hid models.AutoField(primary_keyTrue)hostname models.CharField(max_length32)ip models.CharField(max_length32)# h2g models.ManyToManyField(Group) class Group(models.Model):gid models.AutoField(primary_keyTrue)name models.CharField(max_length16)h2g models.ManyToManyField(Host,throughHostToGroup) # 重点在这class HostToGroup(models.Model):hgid models.AutoField(primary_keyTrue)host_id models.ForeignKey(Host)group_id models.ForeignKey(Group)status models.IntegerField()class Meta:# index_together (host_id,goup_id)unique_together [(host_id, group_id),]本次操作以第二种表结构操作第一种方式传统的操作方式和一对多操作一样第二三种主要是通关表结构中many来操作表 2、创建数据 def index(request):models.Host.objects.create(hostnamec1, ip1.1.1.1)models.Host.objects.create(hostnamec2, ip1.1.1.2)models.Host.objects.create(hostnamec3, ip1.1.1.3)models.Host.objects.create(hostnamec4, ip1.1.1.4)models.Host.objects.create(hostnamec5, ip1.1.1.5)models.Group.objects.create(name财务部)models.Group.objects.create(name人事部)models.Group.objects.create(name公关部)models.Group.objects.create(name技术部)models.Group.objects.create(name运营部)models.Group.objects.create(name销售部)models.Group.objects.create(name客服部)return HttpResponse(OK)3、正向添加数据 obj models.Group.objects.get(gid1 )obj.h2g.add(*models.Host.objects.all())print(obj.h2g.all())# QuerySet [Host: Host object, Host: Host object, Host: Host object, Host: Host object, Host: Host object]4、反向添加数据_set # 将一台机器分配给多个组 # 方法一 只能一个一个添加host models.Host.objects.get(hid1)group models.Group.objects.get(gid4)group.h2g.add(host)# 方法二 添加多条h models.Host.objects.get(hid4)h.group_set.add(*models.Group.objects.filter(gid__gt3))# 方法三h models.Host.objects.get(hid2)ret h.group_set.set(models.Group.objects.filter(gid__gt3))print(ret,type(ret))5、删除 # 删除语句h models.Host.objects.get(hid2)h.group_set.remove(*models.Group.objects.filter(gid__gt3))# 危险删除方法慎用会把分组表里的分组也会给删掉h.group_set.all().delete()6、补充 # 补充add添加可以添加数字h models.Host.objects.get(hid1)h.group_set.add(1)h.group_set.add(models.Group.objects.get(gid1))h.group_set.add(*[2,3])h.group_set.add(*models.Group.objects.filter(gid__gt1))# 两个表都添加数据h models.Host.objects.get(hid2)h.group_set.get_or_create(name 人事部)h.group_set.update_or_create(name技术部)
http://wiki.neutronadmin.com/news/251187/

相关文章:

  • 怎么新增网站推广windows优化大师有必要安装吗
  • 莒县网站设计高安网站制作
  • wordpress做门户网站免费素材图库
  • 手机网站如何做优化手机网站制作公司
  • 网站排名费用专业做网站建设的公司
  • 阿里巴巴企业网站怎么做如何在手机上做广告
  • 咸阳seo优化seo行业网
  • 商城网站怎么做推广方案北京软件外包公司
  • 主流网站模板外国的网站 ftp
  • 用云怎么做网站百度400电话
  • 基本的网站开发技术网络工程规划与设计案例
  • 网址跳转网站PHP网站开发方向
  • 做母婴产品哪个网站做的好处江苏建设信息电子证书查询
  • 黄冈网站seo应用商店网站模板
  • 企业网站seo报价wordpress本地音乐
  • 做公众号的必备参考网站opensns wordpress
  • 友情链接网站免费自己做公司网站难吗
  • 访问量大的网站选择多少流量的服务器何时十大免费实用网站
  • sirna在线设计网站广告设计公司招聘
  • wordpress4.7.5网络推广优化方法
  • 兰州市城乡建设局网官网站自豪地采用wordpress 怎么去掉
  • 网站在阿里云备案韩国手表网站
  • 网站建设国家标准房地产网站系统
  • 帮我们做网站的人找不到了做图的模板下载网站有哪些
  • 国内用react做的网站长沙做网站建设公司哪家好
  • 商水县建设局网站wordpress 文章标题字体
  • 替人做非法网站内蒙古住房与建设官方网站
  • 12306网站开发成本档案室建设网站
  • 免费建商城网站wordpress mu 插件
  • 免费建站个人网站king cms网站建设