Esempio n. 1
0
def validate(cls, model):
    """
    Does basic ModelAdmin option validation. Calls custom validation
    classmethod in the end if it is provided in cls. The signature of the
    custom validation classmethod should be: def validate(cls, model).
    """
    # Before we can introspect models, they need to be fully loaded so that
    # inter-relations are set up correctly. We force that here.
    models.get_apps()

    opts = model._meta
    validate_base(cls, model)

    # list_display
    if hasattr(cls, 'list_display'):
        check_isseq(cls, 'list_display', cls.list_display)
        for idx, field in enumerate(cls.list_display):
            if not callable(field):
                if not hasattr(cls, field):
                    if not hasattr(model, field):
                        try:
                            opts.get_field(field)
                        except models.FieldDoesNotExist:
                            raise ImproperlyConfigured("%s.list_display[%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))
                    else:
                        # getattr(model, field) could be an X_RelatedObjectsDescriptor
                        f = fetch_attr(cls, model, opts, "list_display[%d]" % idx, field)
                        if isinstance(f, models.ManyToManyField):
                            raise ImproperlyConfigured("'%s.list_display[%d]', '%s' is a ManyToManyField which is not supported."
                                % (cls.__name__, idx, field))

    # list_display_links
    if hasattr(cls, 'list_display_links'):
        check_isseq(cls, 'list_display_links', cls.list_display_links)
        for idx, field in enumerate(cls.list_display_links):
            if field not in cls.list_display:
                raise ImproperlyConfigured("'%s.list_display_links[%d]' "
                        "refers to '%s' which is not defined in 'list_display'."
                        % (cls.__name__, idx, field))

    # list_filter
    if hasattr(cls, 'list_filter'):
        check_isseq(cls, 'list_filter', cls.list_filter)
        for idx, fpath in enumerate(cls.list_filter):
            try:
                get_fields_from_path(model, fpath)
            except (NotRelationField, FieldDoesNotExist), e:
                raise ImproperlyConfigured(
                    "'%s.list_filter[%d]' refers to '%s' which does not refer to a Field." % (
                        cls.__name__, idx, fpath
                    )
                )
Esempio n. 2
0
 def get_filters(self, request):
     filter_specs = []
     if self.list_filter:
         for filter_name in self.list_filter:
             field = get_fields_from_path(self.model, filter_name)[-1]
             spec = FilterSpec.create(field, request, self.params,
                                      self.model, self.model_admin,
                                      field_path=filter_name)
             if spec and spec.has_output():
                 filter_specs.append(spec)
     return filter_specs, bool(filter_specs)