Django之modles(模型)
Posted 2mei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Django之modles(模型)的主要内容,如果未能解决你的问题,请参考以下文章