django中@property装饰器的运用
python提供的内置装饰器——staticmethod、classmethod和property
在OSQA中,@property的使用频率是非常高的。下面就是它的使用方法:
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,和java中的setter和getter类似。
models.py中如下:
from django.db import models class Person(models.Model):
G=(('chen','jian'),('hong','yi'),('rt','ju'))
gender=models.CharField(max_length=20,choices=G) @property
def Gender(self):
return self.gender @Gender.setter
def Gender(self,new_value):
self.gender=new_value
在views.py中使用:
from django.http import HttpResponse
from mytest.models import *
def index(request):
print Person.objects.all()[0].Gender
b=Person.objects.all()[0]
b.Gender='adfasfasd'
print b.Gender
b.save()
return HttpResponse(Person.objects.all()[0].Gender)
@property提供的是一个只读的属性,如果需要对属性进行修改,那么就需要定义它的setter。
django中@property装饰器的运用的更多相关文章
- python中@property装饰器的使用
目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...
- 在django中应用装饰器(一)
在新写的博客应用中,涉及很多关于权限的问题,比如修改用户信息,博客的修改与删除,虽然默认的提交信息都是session的用户,但是也应该防止一下篡改提交的可能,之前想的是在每个view中加一段判断的逻辑 ...
- django ----CBV中加装饰器
CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...
- 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解
第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一. 引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...
- 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...
- Python的property装饰器的基本用法
Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...
- 面向对象之组合、封装、多态、property装饰器
概要: 组合 封装 property装饰器 多态 Python推崇鸭子类型:解耦合,统一标准(不用继承) 1. 组合 继承:会传递给子类强制属性 组合:解耦合,减少占用内存.如:正常继承,如果一个班级 ...
- import导入模块,==和is,浅拷贝和深拷贝,进制转换,位运算,私有化,property装饰器
'''import导入模块'''import sysprint(sys.path) sys.path.append('D://ASoft/Python/PycharmProjects')import ...
- property装饰器
# 需要了解的property的用法 class People: def __init__(self,name): self.__name=name @property def name(self): ...
随机推荐
- HTTP/2协议
一.简介 http://toutiao.com/a6258917202906104066/
- DEPENDS工具和DUMPBIN工具使用
在系统部署运行时我们经常发现某个程序在开发机器中可以运行,但是部署在某台PC上缺不能运行,也存在在某些机器上可运行换一台机器却不能运行.主要表现出两种现象: (1).运行.调试时出现程序 ...
- Python监控日志程序-乾颐堂
一个简易的日志监控的脚本,功能如下:1.windows环境2.当匹配日志关键字时会发出声音,匹配的关键字不同,播放的声音不同3.能做到实时响应 注意:是在win环境下哦 直接上代码吧 1 2 3 4 ...
- No member named 'setResizeMode' in 'QHeaderView' - Convert Qt 4.7 to Qt 5.8
https://stackoverflow.com/questions/42743141/no-member-named-setresizemode-in-qheaderview-convert-qt ...
- Spring Boot☞ 多数据源配置(二):Spring-data-jpa
效果图: 代码区: package com.wls.integrateplugs.datasource; import org.springframework.beans.factory.annota ...
- 查看ubuntu 14.04的网关和DNS
root@ubuntu:~# nm-tool NetworkManager Tool State: connected (global) - Device: eth0 [自动以太网] -------- ...
- 全面剖析Smarty缓存机制二[清除缓存方法]
前段时间,写了一篇 Smaryt缓存机制的几种缓存方式 ,详细介绍了三种缓存方式:全局缓存.部分缓存.局部缓存,以及通过is_cache()判断是否存在缓存来进行缓存生成.本来这篇早该完成,由于时间关 ...
- Word文档发布到CSDN博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- Unity开发小技巧整理
- 使用GeoServer+QGIS发布WMTS服务 | Publishing WMTS Service Using GeoServer+QGIS
Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...