本文整理汇总了Python中django.db.models.FieldDoesNotExist方法的典型用法代码示例。如果您正苦于以下问题:Python models.FieldDoesNotExist方法的具体用法?Python models.FieldDoesNotExist怎么用?Python models.FieldDoesNotExist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.FieldDoesNotExist方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_ordering_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def get_ordering_field(self, field_name):
"""
Returns the proper model field name corresponding to the given
field_name to use for ordering. field_name may either be the name of a
proper model field or the name of a method (on the admin or model) or a
callable with the 'admin_order_field' attribute. Returns None if no
proper model field name can be matched.
"""
try:
field = self.opts.get_field(field_name)
return field.name
except models.FieldDoesNotExist:
# See whether field_name is a name of a non-field
# that allows sorting.
if callable(field_name):
attr = field_name
elif hasattr(self, field_name):
attr = getattr(self, field_name)
else:
attr = getattr(self.model, field_name)
return getattr(attr, 'admin_order_field', None)
示例2: _get_field_aggregate
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def _get_field_aggregate(self, field_name, obj, row):
item = ResultItem(field_name, row)
item.classes = ['aggregate', ]
if field_name not in self.aggregate_fields:
item.text = ""
else:
try:
f = self.opts.get_field(field_name)
agg_method = self.aggregate_fields[field_name]
key = '%s__%s' % (field_name, agg_method)
if key not in obj:
item.text = ""
else:
item.text = display_for_field(obj[key], f)
item.wraps.append('%%s<span class="aggregate_title label label-info">%s</span>' % AGGREGATE_TITLE[agg_method])
item.classes.append(agg_method)
except FieldDoesNotExist:
item.text = ""
return item
示例3: validate_local_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def validate_local_fields(e, opts, field_name, fields):
from django.db import models
if not isinstance(fields, collections.Sequence):
e.add(opts, 'all %s elements must be sequences' % field_name)
else:
for field in fields:
try:
f = opts.get_field(field, many_to_many=True)
except models.FieldDoesNotExist:
e.add(opts, '"%s" refers to %s, a field that doesn\'t exist.' % (field_name, field))
else:
if isinstance(f.rel, models.ManyToManyRel):
e.add(opts, '"%s" refers to %s. ManyToManyFields are not supported in %s.' % (field_name, f.name, field_name))
if f not in opts.local_fields:
e.add(opts, '"%s" refers to %s. This is not in the same model as the %s statement.' % (field_name, f.name, field_name))
示例4: __setattr__
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def __setattr__(self, name, value):
"""
We have to intercept here because RelatedFields won't accept
wrapped model instances values, so we have to trick them.
"""
if name in self._not_wrapped:
return super().__setattr__(name, value)
try:
field = self._obj._meta.get_field(name)
except FieldDoesNotExist:
pass
else:
if field.many_to_one and name != field.attname and value is not None:
if isinstance(value, models.Model):
value = self._wrap_instance(value)
if not isinstance(value, ModelInstanceWrapper):
raise ValueError('value has to be None or ModelInstanceWrapper')
setattr(self._obj, name, value._obj)
field.set_cached_value(self._obj, value)
return
super().__setattr__(name, value)
示例5: attrlabel
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def attrlabel(widget, attrname):
widget_prop = _method_label(widget, attrname)
if widget_prop is not None:
return widget_prop
elif widget.model:
model_prop = _method_label(widget.model, attrname)
if model_prop is not None:
# Allows to have empty description
return model_prop
if attrname == 'pk':
fieldname = widget.model._meta.pk.name
else:
fieldname = attrname
try:
field = widget.model._meta.get_field(fieldname)
return field.verbose_name
except models.FieldDoesNotExist:
pass
return attrname
示例6: get_list_queryset
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def get_list_queryset(self):
"""
Get model queryset. The query has been filted and ordered.
"""
# First, get queryset from base class.
queryset = self.queryset()
# Use select_related() if one of the list_display options is a field
# with a relationship and the provided queryset doesn't already have
# select_related defined.
if not queryset.query.select_related:
if self.list_select_related:
queryset = queryset.select_related()
elif self.list_select_related is None:
related_fields = []
for field_name in self.list_display:
try:
field = self.opts.get_field(field_name)
except models.FieldDoesNotExist:
pass
else:
if isinstance(field.rel, models.ManyToOneRel):
related_fields.append(field_name)
if related_fields:
queryset = queryset.select_related(*related_fields)
else:
pass
# Then, set queryset ordering.
queryset = queryset.order_by(*self.get_ordering())
# Return the queryset.
return queryset
# List ordering
示例7: get_form_datas
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def get_form_datas(self):
# Prepare the dict of initial data from the request.
# We have to special-case M2Ms as a list of comma-separated PKs.
if self.request_method == 'get':
initial = dict(self.request.GET.items())
for k in initial:
try:
f = self.opts.get_field(k)
except models.FieldDoesNotExist:
continue
if isinstance(f, models.ManyToManyField):
initial[k] = initial[k].split(",")
return {'initial': initial}
else:
return {'data': self.request.POST, 'files': self.request.FILES}
示例8: lookup_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def lookup_field(name, obj, model_admin=None):
opts = obj._meta
try:
f = opts.get_field(name)
except models.FieldDoesNotExist:
# For non-field values, the value is either a method, property or
# returned via a callable.
if callable(name):
attr = name
value = attr(obj)
elif (
model_admin is not None
and hasattr(model_admin, name)
and name not in ('__str__', '__unicode__')
):
attr = getattr(model_admin, name)
value = attr(obj)
else:
if is_rel_field(name, obj):
parts = name.split("__")
rel_name, sub_rel_name = parts[0], "__".join(parts[1:])
rel_obj = getattr(obj, rel_name)
if rel_obj is not None:
return lookup_field(sub_rel_name, rel_obj, model_admin)
attr = getattr(obj, name)
if callable(attr):
value = attr()
else:
value = attr
f = None
else:
attr = None
value = getattr(obj, name)
return f, attr, value
示例9: _get_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def _get_field(field_name: str, model: models.Model) -> Optional[models.Field]:
try:
# noinspection PyProtectedMember
return model._meta.get_field(field_name)
except models.FieldDoesNotExist:
return None
示例10: get_queryset
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import FieldDoesNotExist [as 别名]
def get_queryset(self, args):
qs = (self.model.objects.filter(id__in=args)
if args
else self.model.objects.all())
try:
self.model._meta.get_field('last_updated')
qs = itertools.chain(qs.filter(last_updated=None),
qs.order_by('last_updated'))
except FieldDoesNotExist:
pass
return qs