zoukankan      html  css  js  c++  java
  • 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

    ----

  • 相关阅读:
    找水王
    用户体验评价
    人件集阅读笔记01
    第十四周学习进度
    第十三周学习进度
    第十二周学习进度
    第十一周学习进度
    梦断代码阅读笔记03
    团队项目-第一阶段冲刺-10
    Shell按行读取文件的3种方法
  • 原文地址:https://www.cnblogs.com/JiangLe/p/7954523.html
Copyright © 2011-2022 走看看