当前位置: 首页>>代码示例>>Python>>正文


Python exceptions.FieldDoesNotExist方法代码示例

本文整理汇总了Python中django.core.exceptions.FieldDoesNotExist方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.FieldDoesNotExist方法的具体用法?Python exceptions.FieldDoesNotExist怎么用?Python exceptions.FieldDoesNotExist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.core.exceptions的用法示例。


在下文中一共展示了exceptions.FieldDoesNotExist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _check_raw_id_fields_item

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_raw_id_fields_item(self, cls, model, field_name, label):
        """ Check an item of `raw_id_fields`, i.e. check that field named
        `field_name` exists in model `model` and is a ForeignKey or a
        ManyToManyField. """

        try:
            field = model._meta.get_field(field_name)
        except FieldDoesNotExist:
            return refer_to_missing_field(field=field_name, option=label,
                                          model=model, obj=cls, id='admin.E002')
        else:
            if not isinstance(field, (models.ForeignKey, models.ManyToManyField)):
                return must_be('a ForeignKey or ManyToManyField',
                               option=label, obj=cls, id='admin.E003')
            else:
                return [] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:18,代码来源:checks.py

示例2: _check_radio_fields_key

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_radio_fields_key(self, cls, model, field_name, label):
        """ Check that a key of `radio_fields` dictionary is name of existing
        field and that the field is a ForeignKey or has `choices` defined. """

        try:
            field = model._meta.get_field(field_name)
        except FieldDoesNotExist:
            return refer_to_missing_field(field=field_name, option=label,
                                          model=model, obj=cls, id='admin.E022')
        else:
            if not (isinstance(field, models.ForeignKey) or field.choices):
                return [
                    checks.Error(
                        "The value of '%s' refers to '%s', which is not an "
                        "instance of ForeignKey, and does not have a 'choices' definition." % (
                            label, field_name
                        ),
                        hint=None,
                        obj=cls,
                        id='admin.E023',
                    )
                ]
            else:
                return [] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:26,代码来源:checks.py

示例3: _check_readonly_fields_item

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_readonly_fields_item(self, cls, model, field_name, label):
        if callable(field_name):
            return []
        elif hasattr(cls, field_name):
            return []
        elif hasattr(model, field_name):
            return []
        else:
            try:
                model._meta.get_field(field_name)
            except FieldDoesNotExist:
                return [
                    checks.Error(
                        "The value of '%s' is not a callable, an attribute of '%s', or an attribute of '%s.%s'." % (
                            label, cls.__name__, model._meta.app_label, model._meta.object_name
                        ),
                        hint=None,
                        obj=cls,
                        id='admin.E035',
                    )
                ]
            else:
                return [] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:25,代码来源:checks.py

示例4: _check_date_hierarchy

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_date_hierarchy(self, cls, model):
        """ Check that date_hierarchy refers to DateField or DateTimeField. """

        if cls.date_hierarchy is None:
            return []
        else:
            try:
                field = model._meta.get_field(cls.date_hierarchy)
            except FieldDoesNotExist:
                return refer_to_missing_field(option='date_hierarchy',
                                              field=cls.date_hierarchy,
                                              model=model, obj=cls, id='admin.E127')
            else:
                if not isinstance(field, (models.DateField, models.DateTimeField)):
                    return must_be('a DateField or DateTimeField', option='date_hierarchy',
                                   obj=cls, id='admin.E128')
                else:
                    return [] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:20,代码来源:checks.py

示例5: validate_readonly_fields

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def validate_readonly_fields(self, cls, model):
        " Validate that readonly_fields refers to proper attribute or field. "
        if hasattr(cls, "readonly_fields"):
            check_isseq(cls, "readonly_fields", cls.readonly_fields)
            for idx, field in enumerate(cls.readonly_fields):
                if not callable(field):
                    if not hasattr(cls, field):
                        if not hasattr(model, field):
                            try:
                                model._meta.get_field(field)
                            except FieldDoesNotExist:
                                raise ImproperlyConfigured(
                                    "%s.readonly_fields[%d], %r is not a callable or "
                                    "an attribute of %r or found in the model %r."
                                    % (cls.__name__, idx, field, cls.__name__, model._meta.object_name)
                                ) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:18,代码来源:validation.py

示例6: _check_latitude_field

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_latitude_field(self):
        opts = self.model._meta
        try:
            opts.get_field(self.latitude_field_name)
        except FieldDoesNotExist:
            return [
                checks.Error(
                    "The OSMField '%s' references the non-existent latitude field '%s'."
                    % (self.name, self.latitude_field_name,),
                    hint=None,
                    obj=self,
                    id="osm_field.E001",
                )
            ]
        else:
            return [] 
开发者ID:MarkusH,项目名称:django-osm-field,代码行数:18,代码来源:fields.py

示例7: _check_longitude_field

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_longitude_field(self):
        opts = self.model._meta
        try:
            opts.get_field(self.longitude_field_name)
        except FieldDoesNotExist:
            return [
                checks.Error(
                    "The OSMField '%s' references the non-existent "
                    "longitude field '%s'." % (self.name, self.longitude_field_name,),
                    hint=None,
                    obj=self,
                    id="osm_field.E002",
                )
            ]
        else:
            return [] 
开发者ID:MarkusH,项目名称:django-osm-field,代码行数:18,代码来源:fields.py

示例8: get_field

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def get_field(self, field_name):
        """
        Return a field instance given the name of a forward or reverse field.
        """
        try:
            # In order to avoid premature loading of the relation tree
            # (expensive) we prefer checking if the field is a forward field.
            return self._forward_fields_map[field_name]
        except KeyError:
            # If the app registry is not ready, reverse fields are
            # unavailable, therefore we throw a FieldDoesNotExist exception.
            if not self.apps.models_ready:
                raise FieldDoesNotExist(
                    "%s has no field named '%s'. The app cache isn't ready yet, "
                    "so if this is an auto-created related field, it won't "
                    "be available yet." % (self.object_name, field_name)
                )

        try:
            # Retrieve field instance by name from cached or just-computed
            # field map.
            return self.fields_map[field_name]
        except KeyError:
            raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name)) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:26,代码来源:options.py

示例9: _check_to_fields_exist

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_to_fields_exist(self):
        # Skip nonexistent models.
        if isinstance(self.remote_field.model, str):
            return []

        errors = []
        for to_field in self.to_fields:
            if to_field:
                try:
                    self.remote_field.model._meta.get_field(to_field)
                except exceptions.FieldDoesNotExist:
                    errors.append(
                        checks.Error(
                            "The to_field '%s' doesn't exist on the related "
                            "model '%s'."
                            % (to_field, self.remote_field.model._meta.label),
                            obj=self,
                            id='fields.E312',
                        )
                    )
        return errors 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:23,代码来源:related.py

示例10: lookup_field

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def lookup_field(name, obj, model_admin=None):
    opts = obj._meta
    try:
        f = _get_non_gfk_field(opts, name)
    except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
        # 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 != '__str__':
            attr = getattr(model_admin, name)
            value = attr(obj)
        else:
            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:reBiocoder,项目名称:bioforum,代码行数:26,代码来源:utils.py

示例11: _get_non_gfk_field

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _get_non_gfk_field(opts, name):
    """
    For historical reasons, the admin app relies on GenericForeignKeys as being
    "not found" by get_field(). This could likely be cleaned up.

    Reverse relations should also be excluded as these aren't attributes of the
    model (rather something like `foo_set`).
    """
    field = opts.get_field(name)
    if (field.is_relation and
            # Generic foreign keys OR reverse relations
            ((field.many_to_one and not field.related_model) or field.one_to_many)):
        raise FieldDoesNotExist()

    # Avoid coercing <FK>_id fields to FK
    if field.is_relation and not field.many_to_many and hasattr(field, 'attname') and field.attname == name:
        raise FieldIsAForeignKeyColumnName()

    return field 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:21,代码来源:utils.py

示例12: _check_raw_id_fields_item

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_raw_id_fields_item(self, obj, model, field_name, label):
        """ Check an item of `raw_id_fields`, i.e. check that field named
        `field_name` exists in model `model` and is a ForeignKey or a
        ManyToManyField. """

        try:
            field = model._meta.get_field(field_name)
        except FieldDoesNotExist:
            return refer_to_missing_field(field=field_name, option=label,
                                          model=model, obj=obj, id='admin.E002')
        else:
            if not field.many_to_many and not isinstance(field, models.ForeignKey):
                return must_be('a foreign key or a many-to-many field',
                               option=label, obj=obj, id='admin.E003')
            else:
                return [] 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:18,代码来源:checks.py

示例13: _check_radio_fields_key

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_radio_fields_key(self, obj, model, field_name, label):
        """ Check that a key of `radio_fields` dictionary is name of existing
        field and that the field is a ForeignKey or has `choices` defined. """

        try:
            field = model._meta.get_field(field_name)
        except FieldDoesNotExist:
            return refer_to_missing_field(field=field_name, option=label,
                                          model=model, obj=obj, id='admin.E022')
        else:
            if not (isinstance(field, models.ForeignKey) or field.choices):
                return [
                    checks.Error(
                        "The value of '%s' refers to '%s', which is not an "
                        "instance of ForeignKey, and does not have a 'choices' definition." % (
                            label, field_name
                        ),
                        obj=obj.__class__,
                        id='admin.E023',
                    )
                ]
            else:
                return [] 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:25,代码来源:checks.py

示例14: _check_prepopulated_fields_key

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_prepopulated_fields_key(self, obj, model, field_name, label):
        """ Check a key of `prepopulated_fields` dictionary, i.e. check that it
        is a name of existing field and the field is one of the allowed types.
        """

        try:
            field = model._meta.get_field(field_name)
        except FieldDoesNotExist:
            return refer_to_missing_field(field=field_name, option=label,
                                          model=model, obj=obj, id='admin.E027')
        else:
            if isinstance(field, (models.DateTimeField, models.ForeignKey, models.ManyToManyField)):
                return [
                    checks.Error(
                        "The value of '%s' refers to '%s', which must not be a DateTimeField, "
                        "a ForeignKey, a OneToOneField, or a ManyToManyField." % (label, field_name),
                        obj=obj.__class__,
                        id='admin.E028',
                    )
                ]
            else:
                return [] 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:24,代码来源:checks.py

示例15: _check_readonly_fields_item

# 需要导入模块: from django.core import exceptions [as 别名]
# 或者: from django.core.exceptions import FieldDoesNotExist [as 别名]
def _check_readonly_fields_item(self, obj, model, field_name, label):
        if callable(field_name):
            return []
        elif hasattr(obj, field_name):
            return []
        elif hasattr(model, field_name):
            return []
        else:
            try:
                model._meta.get_field(field_name)
            except FieldDoesNotExist:
                return [
                    checks.Error(
                        "The value of '%s' is not a callable, an attribute of '%s', or an attribute of '%s.%s'." % (
                            label, obj.__class__.__name__, model._meta.app_label, model._meta.object_name
                        ),
                        obj=obj.__class__,
                        id='admin.E035',
                    )
                ]
            else:
                return [] 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:24,代码来源:checks.py


注:本文中的django.core.exceptions.FieldDoesNotExist方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。