django中如何实现model对象加入装饰器
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章将为大家详细讲解有关django中如何实现model对象加入装饰器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。#在现有的对象加入日期修改Mixinclas
千家信息网最后更新 2025年11月08日django中如何实现model对象加入装饰器
这篇文章将为大家详细讲解有关django中如何实现model对象加入装饰器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
#在现有的对象加入日期修改Mixin
class Person(CreationModificationDateMixin):
多出字段:
| created | datetime(6) | NO | | NULL | | | modified | datetime(6) | YES | | NULL | | |
#加入url MinIn
class Person(UrlMixin):
#加入原数据MInxIn
class Person(MetaTagsMixin):
多出字段:
| meta_keywords | varchar(255) | NO | | NULL | | | meta_description | varchar(255) | NO | | NULL | | | meta_author | varchar(255) | NO | | NULL | | | meta_copyright | varchar(255) | NO | | NULL | | |
# -*- coding: UTF-8 -*-from __future__ import unicode_literalsimport urlparsefrom django.conf import settingsfrom django.utils.timezone import now as timezone_nowfrom django.utils.safestring import mark_safefrom django.template.defaultfilters import escapefrom django.db import modelsfrom django.utils.translation import ugettext_lazy as _from django.contrib.contenttypes.models import ContentTypefrom django.contrib.contenttypes.fields import GenericForeignKeyfrom django.core.exceptions import FieldError### See recipe "Model mixin with URL-related methods"class UrlMixin(models.Model): """ A replacement for get_absolute_url() Models extending this mixin should have either get_url or get_url_path implemented. http://code.djangoproject.com/wiki/ReplacingGetAbsoluteUrl """ class Meta: abstract = True def get_url(self): if hasattr(self.get_url_path, "dont_recurse"): raise NotImplementedError try: path = self.get_url_path() except NotImplementedError: raise website_url = getattr(settings, "DEFAULT_WEBSITE_URL", "http://127.0.0.1:8000") return website_url + path get_url.dont_recurse = True def get_url_path(self): if hasattr(self.get_url, "dont_recurse"): raise NotImplementedError try: url = self.get_url() except NotImplementedError: raise bits = urlparse.urlparse(url) return urlparse.urlunparse(("", "") + bits[2:]) get_url_path.dont_recurse = True def get_absolute_url(self): return self.get_url_path()### See recipe "Model mixin handling creation and modification dates"class CreationModificationDateMixin(models.Model): """ Abstract base class with a creation and modification date and time """ created = models.DateTimeField( _("creation date and time"), editable=False, ) modified = models.DateTimeField( _("modification date and time"), null=True, editable=False, ) def save(self, *args, **kwargs): if not self.pk: self.created = timezone_now() else: # To ensure that we have a creation data always, we add this one if not self.created: self.created = timezone_now() self.modified = timezone_now() super(CreationModificationDateMixin, self).save(*args, **kwargs) save.alters_data = True class Meta: abstract = True### See recipe "Model mixin taking care of meta tags"class MetaTagsMixin(models.Model): """ Abstract base class for meta tags in the section """ meta_keywords = models.CharField( _("Keywords"), max_length=255, blank=True, help_text=_("Separate keywords by comma."), ) meta_description = models.CharField( _("Description"), max_length=255, blank=True, ) meta_author = models.CharField( _("Author"), max_length=255, blank=True, ) meta_copyright = models.CharField( _("Copyright"), max_length=255, blank=True, ) class Meta: abstract = True def get_meta_keywords(self): meta_tag = "" if self.meta_keywords: meta_tag = """\n""" % escape(self.meta_keywords) return mark_safe(meta_tag) def get_meta_description(self): meta_tag = "" if self.meta_description: meta_tag = """\n""" % escape(self.meta_description) return mark_safe(meta_tag) def get_meta_author(self): meta_tag = "" if self.meta_author: meta_tag = """\n""" % escape(self.meta_author) return mark_safe(meta_tag) def get_meta_copyright(self): meta_tag = "" if self.meta_copyright: meta_tag = """\n""" % escape(self.meta_copyright) return mark_safe(meta_tag) def get_meta_tags(self): return mark_safe("".join(( self.get_meta_keywords(), self.get_meta_description(), self.get_meta_author(), self.get_meta_copyright(), )))### See recipe "Model mixin handling generic relations"def object_relation_mixin_factory( prefix=None, prefix_verbose=None, add_related_name=False, limit_content_type_choices_to={}, limit_object_choices_to={}, is_required=False, ): """ returns a mixin class for generic foreign keys using "Content type - object Id" with dynamic field names. This function is just a class generator Parameters: prefix : a prefix, which is added in front of the fields prefix_verbose : a verbose name of the prefix, used to generate a title for the field column of the content object in the Admin. add_related_name : a boolean value indicating, that a related name for the generated content type foreign key should be added. This value should be true, if you use more than one ObjectRelationMixin in your model. The model fields are created like this: <>_content_type : Field name for the "content type" <>_object_id : Field name for the "object Id" <>_content_object : Field name for the "content object" """ p = "" if prefix: p = "%s_" % prefix content_type_field = "%scontent_type" % p object_id_field = "%sobject_id" % p content_object_field = "%scontent_object" % p class TheClass(models.Model): class Meta: abstract = True if add_related_name: if not prefix: raise FieldError("if add_related_name is set to True, a prefix must be given") related_name = prefix else: related_name = None optional = not is_required ct_verbose_name = ( _("%s's type (model)") % prefix_verbose if prefix_verbose else _("Related object's type (model)") ) content_type = models.ForeignKey( ContentType, verbose_name=ct_verbose_name, related_name=related_name, blank=optional, null=optional, help_text=_("Please select the type (model) for the relation, you want to build."), limit_choices_to=limit_content_type_choices_to, ) fk_verbose_name = (prefix_verbose or _("Related object")) object_id = models.CharField( fk_verbose_name, blank=optional, null=False, help_text=_("Please enter the ID of the related object."), max_length=255, default="", # for south migrations ) object_id.limit_choices_to = limit_object_choices_to # can be retrieved by MyModel._meta.get_field("object_id").limit_choices_to content_object = GenericForeignKey( ct_field=content_type_field, fk_field=object_id_field, ) TheClass.add_to_class(content_type_field, content_type) TheClass.add_to_class(object_id_field, object_id) TheClass.add_to_class(content_object_field, content_object) return TheClass 关于"django中如何实现model对象加入装饰器"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
对象
篇文章
字段
更多
不错
实用
内容
数据
文章
日期
知识
UTF-8
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
滨州app软件开发哪家靠谱
网络安全进校园的相声
网络技术推广服务包含哪些
sql 关联数据库
电视登陆显示无法解析服务器域名
网络安全手抄报第一名作品
我国工控网络安全形势严峻
普陀区个人存储服务器
建党100周年网络安全总结
校企合作网络安全
2017网络技术高考题
NOKIAE71软件开发
数据库中无符号是什么意思
广州久久玖互联网科技官网
ftp服务器 目录
飞谷互联网科技
dsp的国产软件开发平台
ctf网络安全比赛冠军
数据库数据单元测试
tmb值高于内部数据库8%患者
燃气软件开发有限公司
观网络安全心得体会
文件服务器自动配额
网络安全手抄报照片简单
环路服务器
服务器硬盘不识别怎么回事
长沙网络安全产业规划全文
计算机网络技术广东轻工
服务器配置指纹识别模块
有孚网络安全等级