django -- 为模式增加方法

在django中模式中的方法是行级的、也就是说它操作是表里的行、不是整个表

一、模式定义:

from django.db import models

# Create your models here.
from datetime import datetime,timedelta

class Person(models.Model):
    name = models.CharField(max_length=100)
    birthday=models.DateTimeField()

    def __init__(self,name='welson',birthday=datetime.now()):
        self.name=name
        self.birthday=birthday

    def __str__(self):
        return self.name

    def say_hello(self):
        print("hello I am {0}".format(self.name))

二、行级方法的调用:

python3 manage.py shell
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> django.setup()
>>> from polls.models import Person
>>>
>>> p=Person()
>>> p.say_hello()
hello I am welson

----

原文地址:https://www.cnblogs.com/JiangLe/p/7954523.html