• Django之modles(模型)


    # -*- 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
  • 相关阅读:
    窗体控件JFrame的使用
    WindowBuilder的安装与简介
    Swing事件机制
    Swing的MVC结构
    Swing框架的继承关系
    SWT简介
    Swing简介
    AWT简介
    Java界面设计
    使用Java建立聊天客户端
  • 原文地址:https://www.cnblogs.com/2mei/p/9213527.html
Copyright © 2020-2023  润新知