当前位置: 首页 > article >正文

《Django 5 By Example》阅读笔记:p551-p560

《Django 5 By Example》学习第 20 天,p551-p560 总结,总计 10 页。

一、技术总结

1.custom model field

(1)示例

courses/fields.py

from django.core.exceptions import ObjectDoesNotExist
from django.db import models


class OrderField(models.PositiveIntegerField):
    def __init__(self, for_fields=None, *args, **kwargs):
        self.for_fields = for_fields
        super().__init__(*args, **kwargs)

    def pre_save(self, model_instance, add):
        if getattr(model_instance, self.attname) is None:
            # no current value
            try:
                qs = self.model.objects.all()
                if self.for_fields:
                    query = {
                        field: getattr(model_instance, field)
                        for field in self.for_fields
                    }
                    qs = qs.filter(**query)
                # get the order of the last item
                last_item = qs.latest(self.attname)
                value = getattr(last_item, self.attname) + 1
            except ObjectDoesNotExist:
                value = 0
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super().pre_save(model_instance, add)

courses/models.py:

from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from .fields import OrderField


# Create your models here.

class Subject(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)

    class Meta:
        ordering = ['title']

    def __str__(self):
        return self.title


class Course(models.Model):
    owner = models.ForeignKey(User, related_name='courses_created', on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE, related_name='courses')
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)
    overview = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['created']

    def __str__(self):
        return self.title


class Module(models.Model):
    course = models.ForeignKey(
        Course, related_name='modules', on_delete=models.CASCADE
    )
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    order = OrderField(blank=True, for_fields=['course'])

    class Meta:
        ordering = ['order']

    def __str__(self):
        return f'{self.order}. {self.title}'


class Content(models.Model):
    module = models.ForeignKey(Module, related_name="contents", on_delete=models.CASCADE)
    content_type = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        limit_choices_to={
            'model__in': ('text', 'video', 'image', 'file')
        }
    )
    object_id = models.PositiveIntegerField()
    item = GenericForeignKey('content_type', 'object_id')
    order = OrderField(blank=True, for_fields=['module'])

    class Meta:
        ordering = ['order']


class ItemBase(models.Model):
    owner = models.ForeignKey(User, related_name='%(class)s_related', on_delete=models.CASCADE)
    title = models.CharField(max_length=250)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        # 如果不设置 abstract = True 会有什么影响?
        abstract = True

    def __str__(self):
        return self.title


class Text(ItemBase):
    content = models.TextField()


class File(ItemBase):
    file = models.FileField(upload_to='files')


class Image(ItemBase):
    file = models.ImageField(upload_to='images')


class Video(ItemBase):
    url = models.URLField()

二、英语总结(生词:1)

1.in respect of

也写作 with respect to,意思是“in connection with something(与xxx有关的)”。

class Content(models.Model):

    # ...

    order = OrderField(blank=True, for_fields=['module'])

p557, This time, you specify that the order is calculated with respect to the module field(这一次,指定根据 module 字段计算顺序)。

三、其它

今天也没有什么想说的。

四、参考资料

1. 编程

(1) Antonio Melé,《Django 5 By Example》:https://book.douban.com/subject/37007362/

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org
在这里插入图片描述

欢迎搜索及关注:编程人(a_codists)


http://www.kler.cn/a/443034.html

相关文章:

  • Jenkins触发器--在其他项目执行后构建
  • Lua语言中常用的字符串操作函数
  • 【钉钉在线笔试题】字符串表达式的加减法
  • 【Docker】docker compose 安装 Redis Stack
  • Git:Cherry-Pick 的使用场景及使用流程
  • 2024年华为OD机试真题-判断一组不等式是否满足约束并输出最大差-Python-OD统一考试(E卷)
  • Ubuntu如何下载nvidia驱动和Cuda Toolkit
  • iOS swift开发系列--如何给swiftui内容视图添加背景图片显示
  • 王佩丰24节Excel学习笔记——第十三讲:邮件合并
  • 力扣--LCR 183.望远镜中的最高海拔
  • Linux文件属性 --- 硬链接、所有者、所属组
  • 15.初识接口1 C#
  • thinkphp:try-catch捕获异常
  • flutter --no-color pub get 超时解决方法
  • 51单片机-内部扩展RAM的应用
  • nlp初学者怎么入门?需要学习哪些?
  • MySQL技术:深入理解索引与优化
  • 提升PHP技能:18个实用高级特性
  • Linux docker离线部署
  • 基于Armitage的MSF自动化集成攻击实践
  • Android显示系统(12)- 向SurfaceFlinger申请Buffer
  • 超详细 springboot 整合 Mock 进行单元测试!本文带你搞清楚!
  • 图(dfs与bfs)算法2
  • 如何配置VMware虚拟机的网络,使局域网内其它电脑可以访问?
  • git退掉远程仓库里的某个修改和记录
  • 鸿蒙风起,未来已来——云学堂鸿蒙应用认证开营啦!