Django的model中的Manager对象

Django的model中的Manager对象

当我们在执行ORM的增删改查操作时,一般会这样写:

python 复制代码
from app01 import models

models.Depart.objects.all(..)
models.Depart.objects.filter(..)
models.Depart.objects.create(..)

但是有时候我们需要自定义一些操作来实现某种功能,比如

python 复制代码
models.Depart.objects.xxxxx(...)

这时候我们就需自定义我们的Manager对象

python 复制代码
from django.db import models


class MyManager(models.Manager):
    def xxxxx(self, title):
        models.Depart.objects.create(title=title)
        models.Depart.objects.create(title=title)


class Depart(models.Model):
    title = models.CharField(verbose_name="标题", max_length=32)
    count = models.IntegerField(verbose_name="数量")

    objects = MyManager()

这样就实现了当我们执行models.Depart.objects.xxxxx(...)时,会在表中创建2条数据

相关推荐
果果软件8 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
djjjx.8 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
Wellijohn9 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp
2601_9679801111 小时前
python pytest怎样测试代码
python·pytest·测试·文档·断言
伞伞悦读11 小时前
【第92期】逻辑回归的分数概率与阈值的实验与排查
人工智能·python·深度学习·机器学习
ch62311 小时前
🚀 从零构建本地文档智能审查 Agent:Vibe Coding 实战指南
python
Sashtok12 小时前
cpp基础知识(10)
c++·后端
摇滚侠12 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
CoderYanger12 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
Thanks_ks13 小时前
AI Agent 开发入门:用 Function Calling 让大模型学会 “动手干活”,附订单查询机器人实战
python·ai·大模型·openai·agent·通义千问·mcp