Python中django.db.models.Model方法

Python中django.db.models.Model方法的用法:示例1: model_format_dict123456789101112131415161718# 需要导入模块: from django.db import models [as 别名]# 或者: from django.db.models import Model [as 别名]def model_format_dict(obj):"""Return a `dict` with keys 'verbose_name' and 'verbose_name_plural',typically for use with string formatting.`obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance."""if isinstance(obj, (models.Model, models.base.ModelBase)):opts = obj._metaelif isinstance(obj, models.query.QuerySet):opts = obj.model._metaelse:opts = objreturn {'verbose_name': force_text(opts.verbose_name),'verbose_name_plural': force_text(opts.verbose_name_plural)}示例2: model_ngettext1234567891011121314151617# 需要导入模块: from django.db import models [as 别名]# 或者: from django.db.models import Model [as 别名]def model_ngettext(obj, n=None):"""Return the appropriate `verbose_name` or `verbose_name_plural` value for`obj` depending on the count `n`.`obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.If `obj` is a `QuerySet` instance, `n` is optional and the length of the`QuerySet` is used."""if isinstance(obj, models.query.QuerySet):if n is None:n = obj.count()obj = obj.modeld = model_format_dict(obj)singular, plural = d["verbose_name"], d["verbose_name_plural"]return ungettext(singular, plural, n or 0)

(0)

相关推荐