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装饰器的运用的更多相关文章

  1. python中@property装饰器的使用

    目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...

  2. 在django中应用装饰器(一)

    在新写的博客应用中,涉及很多关于权限的问题,比如修改用户信息,博客的修改与删除,虽然默认的提交信息都是session的用户,但是也应该防止一下篡改提交的可能,之前想的是在每个view中加一段判断的逻辑 ...

  3. django ----CBV中加装饰器

    CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...

  4. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  5. 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析

    一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...

  6. Python的property装饰器的基本用法

    Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...

  7. 面向对象之组合、封装、多态、property装饰器

    概要: 组合 封装 property装饰器 多态 Python推崇鸭子类型:解耦合,统一标准(不用继承) 1. 组合 继承:会传递给子类强制属性 组合:解耦合,减少占用内存.如:正常继承,如果一个班级 ...

  8. import导入模块,==和is,浅拷贝和深拷贝,进制转换,位运算,私有化,property装饰器

    '''import导入模块'''import sysprint(sys.path) sys.path.append('D://ASoft/Python/PycharmProjects')import ...

  9. property装饰器

    # 需要了解的property的用法 class People: def __init__(self,name): self.__name=name @property def name(self): ...

随机推荐

  1. 多个div嵌套,获取鼠标所点击的div对象

    我选择的是冒泡事件 $(function() { $("#主divID").on("click",function(e) {//主div是必须存在的 //冒泡事 ...

  2. 657. Judge Route Circle机器人能否返回

    [抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...

  3. DSA 算法

    一.简介 DSA算法是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS(DigitalSignature Standard).它是一种公开密钥算法,用作数字签名. http:// ...

  4. [C++] decltype(auto) C++ 11 feature

    1 //C++ 11 feature template <class T1, class T2> auto getMultiply(T1 data1, T2 data2) -> de ...

  5. Linux ls命令详解-乾颐堂CCIE

      ls命令用法举例: 例一:列出/home文件夹下的所有文件和目录的详细资料: 1 ls -l -R /home 命令参数之前要有一短横线“-”, 上面的命令也可以这样写: 1 ls -lR /ho ...

  6. laravel form表单提交

    控制器 中间层 中间层

  7. strtotime()

    date('Y-m-d H:i:s',time()) //24小时 date('Y-m-d h:i:s',time()) //12小时

  8. 通过MySql自动同步刷新redis

    在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终的存储引擎,Redis其实也可以作为一种键值对型的数据库,但在一些实际场景中,特别是关系型结构并不适合使用Redis直接作为数据库.这俩家 ...

  9. es-文档版本号,操作类型,分片选择

    一.版本号: 在es中每个文档都有一个版本号,默认情况下,版本号都是随着每次对该文档的修改或者删除自增的,当然你也可以自己指定.有了这个文档号,我们可以像mysql 乐观锁一样,用来进行控制字我们文档 ...

  10. Gym 101190H Hard Refactoring (模拟坑题)

    题意:给定 n 个区间,让你进行合并,问你最后的区间是,如果是空集,输出 false 如果区间是是 [-32768,32767] ,则是true. 析:进行区间合并,要注意,如果是 x >= 0 ...