當前位置: 首頁>>代碼示例>>Python>>正文


Python models.FieldDoesNotExist方法代碼示例

本文整理匯總了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) 
開發者ID:stormsha,項目名稱:StormOnline,代碼行數:23,代碼來源:list.py

示例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 
開發者ID:stormsha,項目名稱:StormOnline,代碼行數:22,代碼來源:aggregation.py

示例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)) 
開發者ID:blackye,項目名稱:luscan-devel,代碼行數:18,代碼來源:validation.py

示例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) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:23,代碼來源:wrappers.py

示例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 
開發者ID:byashimov,項目名稱:django-controlcenter,代碼行數:24,代碼來源:controlcenter_tags.py

示例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 
開發者ID:stormsha,項目名稱:StormOnline,代碼行數:37,代碼來源:list.py

示例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} 
開發者ID:stormsha,項目名稱:StormOnline,代碼行數:17,代碼來源:edit.py

示例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 
開發者ID:stormsha,項目名稱:StormOnline,代碼行數:36,代碼來源:util.py

示例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 
開發者ID:rsinger86,項目名稱:drf-flex-fields,代碼行數:8,代碼來源:filter_backends.py

示例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 
開發者ID:chaoss,項目名稱:prospector,代碼行數:16,代碼來源:base.py


注:本文中的django.db.models.FieldDoesNotExist方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。