我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用django.contrib.admin.options.BaseModelAdmin()。
def decorate_method(self, fn, *args, **kwargs): """Decorate a view method. This is pretty simple - just attach a Gate as a `_gate` property to the function and then wrap it in the method dispatcher. """ @functools.wraps(fn) def dispatcher(*args, **kwargs): largs = list(args) if isinstance(largs[0], BaseModelAdmin): func = functools.partial(fn, largs.pop(0)) else: func = fn request = largs.pop(0) return (self.gate.allow_or_deny(request) or func(request, *largs, **kwargs)) dispatcher._gate = self.gate return dispatcher
def validate_inlines(self, cls, model): " Validate inline model admin classes. " from django.contrib.admin.options import BaseModelAdmin if hasattr(cls, 'inlines'): check_isseq(cls, 'inlines', cls.inlines) for idx, inline in enumerate(cls.inlines): if not issubclass(inline, BaseModelAdmin): raise ImproperlyConfigured("'%s.inlines[%d]' does not inherit " "from BaseModelAdmin." % (cls.__name__, idx)) if not inline.model: raise ImproperlyConfigured("'model' is a required attribute " "of '%s.inlines[%d]'." % (cls.__name__, idx)) if not issubclass(inline.model, models.Model): raise ImproperlyConfigured("'%s.inlines[%d].model' does not " "inherit from models.Model." % (cls.__name__, idx)) inline.validate(inline.model) self.check_inline(inline, model)
def _check_inlines_item(self, obj, model, inline, label): """ Check one inline model admin. """ inline_label = '.'.join([inline.__module__, inline.__name__]) from django.contrib.admin.options import BaseModelAdmin if not issubclass(inline, BaseModelAdmin): return [ checks.Error( "'%s' must inherit from 'BaseModelAdmin'." % inline_label, hint=None, obj=obj.__class__, id='admin.E104', ) ] elif not inline.model: return [ checks.Error( "'%s' must have a 'model' attribute." % inline_label, hint=None, obj=obj.__class__, id='admin.E105', ) ] elif not issubclass(inline.model, models.Model): return must_be('a Model', option='%s.model' % inline_label, obj=obj, id='admin.E106') else: return inline(model, obj.admin_site).check()
def _check_inlines_item(self, obj, model, inline, label): """ Check one inline model admin. """ inline_label = '.'.join([inline.__module__, inline.__name__]) from django.contrib.admin.options import BaseModelAdmin if not issubclass(inline, BaseModelAdmin): return [ checks.Error( "'%s' must inherit from 'BaseModelAdmin'." % inline_label, obj=obj.__class__, id='admin.E104', ) ] elif not inline.model: return [ checks.Error( "'%s' must have a 'model' attribute." % inline_label, obj=obj.__class__, id='admin.E105', ) ] elif not issubclass(inline.model, models.Model): return must_be('a Model', option='%s.model' % inline_label, obj=obj, id='admin.E106') else: return inline(model, obj.admin_site).check()
def __call__(self, fn, *args, **kwargs): """Delegate the decoration to the appropriate method.""" if isinstance(fn, functools.partial) and not hasattr(fn, '__module__'): raise ValueError( 'Cannot decorate a bare functools.partial view. ' 'You must invoke functools.update_wrapper(partial_view, ' 'full_view) first.') if not isinstance(fn, type) and isinstance(fn, collections.Callable): return self.decorate_method(fn, *args, **kwargs) elif isinstance(fn, tuple): # Must be an include('my_app.urls') we're decorating return self.decorate_include(fn, *args, **kwargs) elif isinstance(fn, (RegexURLPattern, RegexURLResolver)): return self.decorate_url_pattern(fn, *args, **kwargs) elif isinstance(fn, type) and issubclass(fn, BaseModelAdmin): if issubclass(fn, InlineModelAdmin): raise TypeError("Cannot decorate Inlines. See " "baya.admin.options.BayaInline instead.") return self.decorate_admin(fn, *args, **kwargs) elif isinstance(fn, six.string_types): raise TypeError("Cannot decorate string-path to view: %s." % fn) else: # You'll probably only get here if you're trying to decorate # a class-based view raise TypeError( "Invalid type for requires decorator (%s). " "You cannot decorate class-based views. Decorate " "the URL or the as_view method instead." % type(fn))
def _check_inlines_item(self, cls, model, inline, label): """ Check one inline model admin. """ inline_label = '.'.join([inline.__module__, inline.__name__]) from django.contrib.admin.options import BaseModelAdmin if not issubclass(inline, BaseModelAdmin): return [ checks.Error( "'%s' must inherit from 'BaseModelAdmin'." % inline_label, hint=None, obj=cls, id='admin.E104', ) ] elif not inline.model: return [ checks.Error( "'%s' must have a 'model' attribute." % inline_label, hint=None, obj=cls, id='admin.E105', ) ] elif not issubclass(inline.model, models.Model): return must_be('a Model', option='%s.model' % inline_label, obj=cls, id='admin.E106') else: return inline.check(model)