在Django的模型中添加自定义方法的示例
作者:goldensun 时间:2021-12-07 17:14:58
为了给你的对像添加一个行级功能,那就定义一个自定义方法。 有鉴于manager经常被用来用一些整表操作(table-wide),模型方法应该只对特殊模型实例起作用。
这是一项在模型的一个地方集中业务逻辑的技术。
最好用例子来解释一下。 这个模型有一些自定义方法:
from django.contrib.localflavor.us.models import USStateField
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
birth_date = models.DateField()
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = USStateField() # Yes, this is U.S.-centric...
def baby_boomer_status(self):
"Returns the person's baby-boomer status."
import datetime
if datetime.date(1945, 8, 1) <= self.birth_date <= datetime.date(1964, 12, 31):
return "Baby boomer"
if self.birth_date < datetime.date(1945, 8, 1):
return "Pre-boomer"
return "Post-boomer"
def is_midwestern(self):
"Returns True if this person is from the Midwest."
return self.state in ('IL', 'WI', 'MI', 'IN', 'OH', 'IA', 'MO')
def _get_full_name(self):
"Returns the person's full name."
return u'%s %s' % (self.first_name, self.last_name)
full_name = property(_get_full_name)
例子中的最后一个方法是一个property。 想了解更多关于属性的信息请访问http://www.python.org/download/releases/2.2/descrintro/#property
这是用法的实例:
>>> p = Person.objects.get(first_name='Barack', last_name='Obama')
>>> p.birth_date
datetime.date(1961, 8, 4)
>>> p.baby_boomer_status()
'Baby boomer'
>>> p.is_midwestern()
True
>>> p.full_name # Note this isn't a method -- it's treated as an attribute
u'Barack Obama'
标签:Django,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
GOOGLE LOGO 设计演化过程
2008-02-13 19:41:00
![](https://img.aspxhome.com/file/UploadPic/up/2008021320182140.gif)
Python变量赋值的秘密分享
2021-10-12 11:53:04
![](https://img.aspxhome.com/file/2023/9/98889_0s.png)
python基础入门之列表(一)
2023-11-23 19:33:42
![](https://img.aspxhome.com/file/2023/9/97249_0s.png)
OpenCV计算平均值cv::mean实例代码
2023-06-19 10:26:02
![](https://img.aspxhome.com/file/2023/3/59833_0s.png)
与 Function 和 Object 相关的有趣代码
2010-05-07 12:29:00
![](https://img.aspxhome.com/file/UploadPic/20105/7/javascript_object_layout-52s.jpg)
Golang排列组合算法问题之全排列实现方法
2023-07-14 14:16:19
如何使用Python多线程测试并发漏洞
2021-11-18 08:25:52
![](https://img.aspxhome.com/file/2023/9/66299_0s.jpg)
什么是XSLT,什么是XPath
2008-01-21 13:12:00
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
2023-08-24 00:25:21
![](https://img.aspxhome.com/file/2023/5/61205_0s.jpg)
python一行输入n个数据问题
2023-09-11 21:50:48
![](https://img.aspxhome.com/file/2023/5/78715_0s.png)
三行Python代码提高数据处理脚本速度
2021-10-15 22:56:54
![](https://img.aspxhome.com/file/2023/3/90533_0s.png)
python读取txt数据的操作步骤
2022-12-27 10:36:31
![](https://img.aspxhome.com/file/2023/2/97102_0s.png)
利用Python操作消息队列RabbitMQ的方法教程
2022-12-11 21:52:16
最近写的一个asp缓存函数
2008-11-25 14:07:00
教你轻松掌握如何保护MySQL中的重要数据
2008-12-19 17:42:00
ORACLE应用经验(1)
2010-07-31 12:53:00
设计需知:移动网站设计应注意的技术细节
2012-04-20 13:03:23
简单且有用的Python数据分析和机器学习代码
2021-03-27 13:13:27
Python内置数据结构与操作符的练习题集锦
2022-07-25 05:12:53
Python 的 f-string 可以连接字符串与数字的原因解析
2023-08-21 15:34:41
![](https://img.aspxhome.com/file/2023/8/70098_0s.png)