码迷,mamicode.com
首页 > 其他好文 > 详细

Django之modles(模型)

时间:2018-06-22 15:33:33      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:man   creat   odi   field   isp   需要   query   orm   splay   

技术分享图片
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

##
class Grades(models.Model):
    gname=models.CharField(max_length=20)
    gdate=models.DateTimeField()
    ggirlnum=models.IntegerField()
    gboynum=models.IntegerField()
    isDelete=models.BooleanField(default=False)
    def __str__(self):
        return "{0},{1},{2},{3}".format(self.gname,self.gdate,self.ggirlnum,self.gboynum)

class StudentsManager(models.Manager):
    def get_queryset(self):
        return super(StudentsManager,self).get_queryset().filter(isDelete=False)
    ## 自定义管理器中添加类方法
    def createStudents(self, name,age,gender,contend,grade,isD=False):
        stu=self.model()
        stu.sname=name
        stu.sage=age
        stu.sgender=gender
        stu.scontend=contend
        stu.sgrade=grade
        return stu  ## 肯定是要return 的,因为views.py中需要接收到此信息,

class Students(models.Model):
    stuObj=StudentsManager()
    objects=models.Manager()
    sname=models.CharField(max_length=20)
    sgender=models.BooleanField(default=True)
    sage=models.IntegerField()
    scontend=models.CharField(max_length=20)
    isDelete=models.BooleanField(default=False)
    sgrade=models.ForeignKey(Grades)  ##关联外键
    def __str__(self):
        return "{0},{1},{2},{3}".format(self.sname,self.sgender,self.sage,self.isDelete)
    # 定义一个类方法用于创建对象
    @classmethod
    def createStudents(cls, name,age,gender,contend,grade,isD=False):
        stu=cls(sname=name,sage=age,sgender=gender,sgrade=grade,isDelete=isD) #创建了一个空对象
        return stu
View Code

 

Django之modles(模型)

标签:man   creat   odi   field   isp   需要   query   orm   splay   

原文地址:https://www.cnblogs.com/2mei/p/9213527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!