python做简单的增删改查

python的框架

django是python的一个快速开发网站的一个框架,在我工作工程中使用,简单的配置网上很多,下面我要说的是django的一个最基本的应用,增上该查,网上的例子很多,
我自己也总结一下,python的这个框架和java相比具有很明显的特点,很轻便,没java那么重,可以快速的开发博客和基本的管理应用,当然逻辑业务还是需要自己去写的
下面是我对python的一些基本理解吧首先是连接数据库,这里可以链接多个数据库很容易实现多库存储,当然也可以使用缓存,这个以后再提,下面菜市场完成对django的理解

##django的增删改查
首先是数据库的orm的持久化设计
···python
class Organization(generic.BO):
“””
组织单位
“””
indexweight = 1
code = models.CharField(
(“organ code”),max_length=const.DB_CHAR_NAME20,blank=True,null=True)
name = models.CharField(
(“organ name”),max_length=const.DB_CHAR_NAME120)
short = models.CharField(
(“short name”),max_length=const.DB_CHAR_NAME20,blank=True,null=True)
pinyin = models.CharField(
(“pinyin”),max_length=const.DB_CHAR_NAME120,blank=True,null=True)
status = models.BooleanField(
(“in use”),default=True)

tax_num = models.CharField(_("tax num"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
tax_address = models.CharField(_("tax address"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
tax_account = models.CharField(_("tax account"),max_length=const.DB_CHAR_NAME_80,blank=True,null=True)

represent = models.CharField(_("representative "),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
address = models.CharField(_("address"),max_length=const.DB_CHAR_NAME_120,blank=True,null=True)
zipcode = models.CharField(_("zipcode"),max_length=const.DB_CHAR_CODE_8,blank=True,null=True)
fax = models.CharField(_("fax"),max_length=const.DB_CHAR_NAME_20,blank=True,null=True)
contacts = models.CharField(_("contacts"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
phone = models.CharField(_("phone"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
website = models.CharField(_("website"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
email = models.CharField(_("email"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
lic_code = models.CharField(_("license code"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
cer_code = models.CharField(_("certificate code"),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
license = models.FileField(_("business license"),blank=True,null=True,upload_to='organ')
certificate = models.FileField(_("organization code certificate"),blank=True,null=True,upload_to='organ')
weight = models.IntegerField(_("weight"),default=9)

class Meta:
    verbose_name = _('organization')
    verbose_name_plural = _('organization')

class OrgUnit(generic.BO):
“””
组织单元
“””
UNITLEVEL = (
(1,
(‘BRANCH’)),
(2,(‘DEPARTMENT’)),
(3,
(‘OFFICE’)),
(4,(‘TEAM’)),
(5,
(‘COMMITTEE’))
)
index_weight = 2
parent = models.ForeignKey(‘self’,verbosename=(“parent”),null=True,blank=True)
organization = models.ForeignKey(Organization,verbosename = (‘organization’),null=True,blank=True)
code = models.CharField(_(“code”),max_length=const.DB_CHAR_CODE8,blank=True,null=True)
name = models.CharField(
(“name”),max_length=const.DB_CHAR_NAME120)
short = models.CharField(
(“short name”),max_length=const.DB_CHAR_NAME20,blank=True,null=True)
pinyin = models.CharField(
(“pinyin”),max_length=const.DB_CHAR_NAME_120,blank=True,null=True)
unittype = models.IntegerField((“type”),choices=UNITLEVEL,default=2)
status = models.BooleanField(
(“in use”),default=True)
virtual = models.BooleanField((“is virtual”),default=False)
fax = models.CharField(
(“fax”),max_length=const.DB_CHAR_NAME20,blank=True,null=True)
phone = models.CharField(
(“phone”),max_length=const.DB_CHAR_NAME40,blank=True,null=True)
contacts = models.CharField(
(“contacts”),max_length=const.DB_CHAR_NAME40,blank=True,null=True)
email = models.CharField(
(“email”),max_length=const.DB_CHAR_NAME40,blank=True,null=True)
weight = models.IntegerField(
(“weight”),default=99)

class Meta:
    verbose_name = _('org unit')
    verbose_name_plural = _('org unit')

···
然后是对数据进行增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
ef add_department(request):
"""
部门添加/修改
"""
data = dict()
id = request.POST.get('department_id')
name = request.POST.get('department_name')
depart_type=request.POST.get('department_type')
try:
# 0 新增
if id == '0':
department = Department()
# 其他:编辑
else:
department = Department.objects.get(pk=id)
department.name = name
department.depart_type=depart_type
department.save()
data['is_succ'] = True
except Department.DoesNotExist:
data['is_succ'] = False
data['msg'] = '未查询到指定部门'
except Exception as e:
data['is_succ'] = False
data['msg'] = e.message
return HttpResponse(json.dumps(data), mimetype='application/json')
@transaction.atomic
@login_required
def del_department(request):
"""删除部门"""
data = dict()
id = request.POST.get('department_id')
try:
UserInfo.objects.filter(position__department_id=id).delete()
Position.objects.filter(department_id=id).delete()
Department.objects.filter(pk=id).delete()
data['is_succ'] = True
except Exception as e:
data['is_succ'] = False
data['msg'] = e.message
return HttpResponse(json.dumps(data), mimetype='application/json')

最后要配置的是路由转发
就是接口的使用和前端做交互,简单明了的mvc设计

##总结
基本的流程就是这样,其实python的增删改查简化和封装了很多细节的东西,让开发者更加关注的是业务逻辑,在两个星期的学习中也遇到了很多的问题,以后我会好好总结和归纳,然后整理,只有不断的学习,才能够继续
前进

文章目录
  1. 1. python的框架
,