class CategoryManager(models.Manager):
"""
A manager that adds an "active()" method for all active categories
"""
def active(self):
"""
Only categories that are active
"""
return self.get_query_set().filter(active=True) class CategoryBase(MPTTModel):
"""
This base model includes the absolute bare bones fields and methods. One
could simply subclass this model and do nothing else and it should work.
"""
parent = TreeForeignKey('self',
blank=True,
null=True,
related_name='children',
verbose_name=_('parent'))
name = models.CharField(max_length=100, verbose_name=_('name'))
slug = models.SlugField(verbose_name=_('slug'))
active = models.BooleanField(default=True, verbose_name=_('active')) objects = CategoryManager()
tree = TreeManager() def save(self, *args, **kwargs):
"""
While you can activate an item without activating its descendants,
It doesn't make sense that you can deactivate an item and have its
decendants remain active.
"""
if not self.slug:
self.slug = slugify(SLUG_TRANSLITERATOR(self.name))[:50] super(CategoryBase, self).save(*args, **kwargs) if not self.active:
for item in self.get_descendants():
if item.active != self.active:
item.active = self.active
item.save() def __unicode__(self):
ancestors = self.get_ancestors()
return ' > '.join([force_unicode(i.name) for i in ancestors] + [self.name, ]) class Meta:
abstract = True
unique_together = ('parent', 'name')
ordering = ('tree_id', 'lft') class MPTTMeta:
order_insertion_by = 'name'

http://www.oschina.net/translate/higher-level-query-api-django-orm


http://www.nanerbang.com/article/34/

这里的貌似也不错

from django.db import models

class PublicArticleManager(models.Manager):
def get_query_set(self):
return super(PublicArticleManager, self).get_query_set().filter(public = True) class Article(models.Model):
#...
public = models.BooleanField('是否发表', default = False) objects = models.Manager()
public_articles = PublicArticleManager()

使用自定义的 Manager的更多相关文章

  1. Django 模型中自定义Manager和模型方法

    1.自定义管理器(Manager) 在语句Book.objects.all()中,objects是一个特殊的属性,通过它来查询数据库,它就是模型的一个Manager. 每个Django模型至少有一个m ...

  2. Django中自定义模型管理器(Manager)及方法

    1.自定义管理器(Manager) 在语句Book.objects.all()中,objects是一个特殊的属性,通过它来查询数据库,它就是模型的一个Manager.每个Django模型至少有一个ma ...

  3. Django 自定义模型管理器(Manager)及方法

    转载自:https://www.cnblogs.com/sui776265233/p/11571418.html 1.自定义管理器(Manager) 在语句Book.objects.all()中,ob ...

  4. django模型manager学习记录

    Managers 在语句Book.objects.all()中,objects是一个特殊的属性,需要通过它查询数据库. 在第5章,我们只是简要地说这是模块的manager .现在是时候深入了解mana ...

  5. Cloudera Manager产品介绍

    一.Cloudera Manager简介 Cloudera Manager(后面简称CM)是CDH(Cloudera’s Distribution Including Apache Hadoop)市场 ...

  6. django manager

    django manager 在语句Book.objects.all()中,objects是一个特殊的属性,需要通过它查询数据库. 总之,模块manager是一个对象,Django模块通过它进行数据库 ...

  7. python 中变量的命名方法

    从网上找到django中python的命名规范 Python  规范 代码的布局  编码 所有的Python脚本文件都应在文件头标上“# -*- coding:utf-8 -*-”.  缩进 4个空格 ...

  8. Django views 中的 shortcut function

    shortcut function都在django.shortcuts这个包中,主要包含有:render(), render_to_response(), redirect(), get_object ...

  9. 【Django】Django model与数据库操作对应关系(转)

    Django对数据库的操作分用到三个类:Manager.QuerySet.Model. Manager的主要功能定义表级方法(表级方法就是影响一条或多条记录的方法),我们可以以models.Manag ...

随机推荐

  1. codeforces 854C.Planning 【贪心/优先队列】

    Planning time limit per test 1 second memory limit per test 512 megabytes input standard input outpu ...

  2. python3之Django表单(一)

    1.HTML中的表单 在HTML种,表单是在<form>...</form>种的元素,它允许用户输入文本,选择选项,操作对象等,然后发送这些数据到服务器 表单元素允许用户在表单 ...

  3. MyBatis学习笔记3--使用XML配置SQL映射器

    <resultMap type="Student" id="StudentResult"> <id property="id&quo ...

  4. 运行程序,解读this指向---case2

    片段1 var anum = 666; function funcTest1(){ var b = anum * 2; var anum = 6; var c = anum / 2; console. ...

  5. AGC027 A - Candy Distribution Again

    目录 题目链接 题解 代码 题目链接 AGC027 A - Candy Distribution Again 题解 贪心即可 代码 #include<cstdio> #include< ...

  6. maven的pom.xml配置文件中常用的配置标签解析(2018-03-13)

    来自:https://www.cnblogs.com/Nick-Hu/p/7288198.html 拿过来记录下 <project xmlns="http://maven.apache ...

  7. cocos2d-x入门学习--准备篇

    1.Cocos2D最早是一款用Python语言开发的游戏引擎.Cocos2D是一个开源框架,用于构建二维游戏,演示程序和其他图形界面交互应用等. 2.x的包含两个意思:一方面是C++的文件扩展为CXX ...

  8. linux常用服务程序一键安装

    PHP7安装 rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm service php-fpm stop yum remove php5* ...

  9. Mac 安装配置nexus2.6 搭建Maven的中央仓库

    今天配置java 环境,安装nexus 百度了好久才安装好,所以特别写下来 分享给同样遇到问题的你.废话不多说,直接上步骤 前置条件 :已经安装了JDK 下载nexus(http://www.sona ...

  10. [Android Pro] Android P版本 新功能介绍和兼容性处理(三)Android Studio 3.0 ~ 3.2 其他特性

    cp : https://blog.csdn.net/yi_master/article/details/80067198 1:JAVA8特性支持 1)Base64.java 在升级到as3.0之后, ...