python的框架
django是python的一个快速开发网站的一个框架,在我工作工程中使用,简单的配置网上很多,下面我要说的是django的一个最基本的应用,增上该查,网上的例子很多,
我自己也总结一下,python的这个框架和java相比具有很明显的特点,很轻便,没java那么重,可以快速的开发博客和基本的管理应用,当然逻辑业务还是需要自己去写的
下面是我对python的一些基本理解吧首先是连接数据库,这里可以链接多个数据库很容易实现多库存储,当然也可以使用缓存,这个以后再提,下面菜市场完成对django的理解
django的增删改查
首先是数据库的orm的持久化设计
···python
class Organization(generic.BO):
“””
组织单位
“””
index_weight = 1
code = models.CharField(_(“organ code”),max_length=const.DB_CHAR_NAME_20,blank=True,null=True)
name = models.CharField(_(“organ name”),max_length=const.DB_CHAR_NAME_120)
short = models.CharField(_(“short name”),max_length=const.DB_CHAR_NAME_20,blank=True,null=True)
pinyin = models.CharField(_(“pinyin”),max_length=const.DB_CHAR_NAME_120,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):
“””
组织单元
“””
UNIT_LEVEL = (
(1,_(‘BRANCH’)),
(2,_(‘DEPARTMENT’)),
(3,_(‘OFFICE’)),
(4,_(‘TEAM’)),
(5,_(‘COMMITTEE’))
)
index_weight = 2
parent = models.ForeignKey(‘self’,verbose_name=_(“parent”),null=True,blank=True)
organization = models.ForeignKey(Organization,verbose_name = _(‘organization’),null=True,blank=True)
code = models.CharField(_(“code”),max_length=const.DB_CHAR_CODE_8,blank=True,null=True)
name = models.CharField(_(“name”),max_length=const.DB_CHAR_NAME_120)
short = models.CharField(_(“short name”),max_length=const.DB_CHAR_NAME_20,blank=True,null=True)
pinyin = models.CharField(_(“pinyin”),max_length=const.DB_CHAR_NAME_120,blank=True,null=True)
unit_type = models.IntegerField(_(“type”),choices=UNIT_LEVEL,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_NAME_20,blank=True,null=True)
phone = models.CharField(_(“phone”),max_length=const.DB_CHAR_NAME_40,blank=True,null=True)
contacts = models.CharField(_(“contacts”),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)
weight = models.IntegerField(_(“weight”),default=99)
class Meta:
verbose_name = _('org unit')
verbose_name_plural = _('org unit')
···
然后是对数据进行增删改查
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的增删改查简化和封装了很多细节的东西,让开发者更加关注的是业务逻辑,在两个星期的学习中也遇到了很多的问题,以后我会好好总结和归纳,然后整理,只有不断的学习,才能够继续前进