您的位置:首页 > 新闻 > 会展 > 山西建筑工程集团有限公司_博山做网站公司_浏览器广告投放_有没有免费的推广网站

山西建筑工程集团有限公司_博山做网站公司_浏览器广告投放_有没有免费的推广网站

2024/12/27 3:59:49 来源:https://blog.csdn.net/codists/article/details/144494526  浏览:    关键词:山西建筑工程集团有限公司_博山做网站公司_浏览器广告投放_有没有免费的推广网站
山西建筑工程集团有限公司_博山做网站公司_浏览器广告投放_有没有免费的推广网站

《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 modelsclass OrderField(models.PositiveIntegerField):def __init__(self, for_fields=None, *args, **kwargs):self.for_fields = for_fieldssuper().__init__(*args, **kwargs)def pre_save(self, model_instance, add):if getattr(model_instance, self.attname) is None:# no current valuetry: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 itemlast_item = qs.latest(self.attname)value = getattr(last_item, self.attname) + 1except ObjectDoesNotExist:value = 0setattr(model_instance, self.attname, value)return valueelse: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 ContentTypefrom .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.titleclass 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.titleclass 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 = Truedef __str__(self):return self.titleclass 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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com