예제 #1
0
 def get_sortable_fields(self, model):
     return sorted(get_field_names_from_opts(model._meta))
예제 #2
0
    def names_to_path(self, names, opts, fail_on_missing=False):  # flake8: noqa
        path = []
        for pos, name in enumerate(names):
            if name == 'pk':
                name = opts.pk.name

            field = None
            try:
                field = opts.get_field(name)
            except FieldDoesNotExist:
                pass

            if field is not None:
                if field.is_relation and not field.related_model:
                    raise NotImplementedError(
                        'Generic relationships not implemented yet'
                    )

                if field.is_relation and not isinstance(field.related_model(), CDMSModel):
                    raise NotImplementedError(
                        'Relations not of type CDMSModel not yet implemented'
                    )

                if field.is_relation and len(names) > 1:
                    # cdms doesn't like 'relatedField/Field' so much but it could potentially
                    # be possible so investigate further if worth.
                    raise NotImplementedError(
                        'Only filtering by foreign key allowed at the moment'
                    )

                try:
                    model = field.model._meta.concrete_model
                except AttributeError:
                    model = None
            else:
                # We didn't find the current field, so move position back
                # one step.
                pos -= 1
                if pos == -1 or fail_on_missing:
                    field_names = list(get_field_names_from_opts(opts))
                    available = sorted(field_names)
                    raise FieldError("Cannot resolve keyword %r into field. "
                                     "Choices are: %s" % (name, ", ".join(available)))
                break

            # Check if we need any joins for concrete inheritance cases (the
            # field lives in parent, but we are currently in one of its
            # children)
            if model is not opts.model:
                raise NotImplementedError(
                    'Proxy objects not yet implemented'
                )

            if hasattr(field, 'get_path_info'):
                pathinfos = field.get_path_info()
                last = pathinfos[-1]
                final_field = last.join_field
                targets = (field,)

                break
            else:
                # Local non-relational field.
                final_field = field
                targets = (field,)
                if fail_on_missing and pos + 1 != len(names):
                    raise FieldError(
                        "Cannot resolve keyword %r into field. Join on '%s'"
                        " not permitted." % (names[pos + 1], name))
                break
        return path, final_field, targets, names[pos + 1:]
예제 #3
0
 def test_get_field_names_from_opts(self):
     self.assertEqual(get_field_names_from_opts(None), set())