我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用django.contrib.admin.options.InlineModelAdmin()。
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 InlineModelAdmin if not issubclass(inline, InlineModelAdmin): return [ checks.Error( "'%s' must inherit from 'InlineModelAdmin'." % 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 media(self): if not isinstance(self, InlineModelAdmin): context = {'field': self.ordering_field} else: if not self.fk_name: raise ImproperlyConfigured( '%r requires a `fk_name` -- we are too dumb/lazy to' ' determine it ourselves. Thanks!' % (self.__class__)) context = { 'field': self.ordering_field, 'prefix': get_default_formset_prefix( self.parent_model, self.model, self.fk_name), 'stacked': isinstance(self, admin.StackedInline), 'tabular': isinstance(self, admin.TabularInline), } return super(OrderableAdmin, self).media + forms.Media( css={'all': ( 'admin_ordering/admin_ordering.css', )}, js=[ 'admin_ordering/jquery-ui-1.11.4.custom.min.js', JS('admin_ordering/admin_ordering.js', { 'class': 'admin-ordering-context', 'data-context': json.dumps(context), }), ], )
def test_inline_decoration(self): # This should fail because inlines don't have any {add,change,delete} # views to protect. with self.assertRaises(TypeError): @requires(g('A')) class MyInline(InlineModelAdmin): pass
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 __init__(self, *args, **kwargs): """Initializes the VersionAdmin""" super(VersionAdmin, self).__init__(*args, **kwargs) # Automatically register models if required. if not self.revision_manager.is_registered(self.model): inline_fields = [] for inline in self.inlines: inline_model = inline.model if issubclass(inline, GenericInlineModelAdmin): ct_field = inline.ct_field ct_fk_field = inline.ct_fk_field for field in self.model._meta.many_to_many: if isinstance(field, GenericRelation) and field.rel.to == inline_model and field.object_id_field_name == ct_fk_field and field.content_type_field_name == ct_field: inline_fields.append(field.name) self._autoregister(inline_model) elif issubclass(inline, options.InlineModelAdmin): fk_name = inline.fk_name if not fk_name: for field in inline_model._meta.fields: if isinstance(field, (models.ForeignKey, models.OneToOneField)) and issubclass(self.model, field.rel.to): fk_name = field.name self._autoregister(inline_model, follow=[fk_name]) if not inline_model._meta.get_field(fk_name).rel.is_hidden(): accessor = inline_model._meta.get_field(fk_name).related.get_accessor_name() inline_fields.append(accessor) self._autoregister(self.model, inline_fields) # Wrap own methods in manual revision management. self.add_view = self.revision_context_manager.create_revision(manage_manually=True)(self.add_view) self.change_view = self.revision_context_manager.create_revision(manage_manually=True)(self.change_view) self.delete_view = self.revision_context_manager.create_revision(manage_manually=True)(self.delete_view) self.recover_view = self.revision_context_manager.create_revision(manage_manually=True)(self.recover_view) self.revision_view = self.revision_context_manager.create_revision(manage_manually=True)(self.revision_view) self.changelist_view = self.revision_context_manager.create_revision(manage_manually=True)(self.changelist_view)