MCPcopy
hub / github.com/django/django / register

Method register

django/contrib/admin/sites.py:95–149  ·  view source on GitHub ↗

Register the given model(s) with the given admin class. The model(s) should be Model classes, not instances. If an admin class isn't given, use ModelAdmin (the default admin options). If keyword arguments are given -- e.g., list_display -- apply them as opt

(self, model_or_iterable, admin_class=None, **options)

Source from the content-addressed store, hash-verified

93 return errors
94
95 def register(self, model_or_iterable, admin_class=None, **options):
96 """
97 Register the given model(s) with the given admin class.
98
99 The model(s) should be Model classes, not instances.
100
101 If an admin class isn't given, use ModelAdmin (the default admin
102 options). If keyword arguments are given -- e.g., list_display --
103 apply them as options to the admin class.
104
105 If a model is already registered, raise AlreadyRegistered.
106
107 If a model is abstract, raise ImproperlyConfigured.
108 """
109 admin_class = admin_class or ModelAdmin
110 if isinstance(model_or_iterable, ModelBase):
111 model_or_iterable = [model_or_iterable]
112 for model in model_or_iterable:
113 if model._meta.abstract:
114 raise ImproperlyConfigured(
115 "The model %s is abstract, so it cannot be registered with admin."
116 % model.__name__
117 )
118 if model._meta.is_composite_pk:
119 raise ImproperlyConfigured(
120 "The model %s has a composite primary key, so it cannot be "
121 "registered with admin." % model.__name__
122 )
123
124 if self.is_registered(model):
125 registered_admin = str(self.get_model_admin(model))
126 msg = "The model %s is already registered " % model.__name__
127 if registered_admin.endswith(".ModelAdmin"):
128 # Most likely registered without a ModelAdmin subclass.
129 msg += "in app %r." % registered_admin.removesuffix(".ModelAdmin")
130 else:
131 msg += "with %r." % registered_admin
132 raise AlreadyRegistered(msg)
133
134 # Ignore the registration if the model has been
135 # swapped out.
136 if not model._meta.swapped:
137 # If we got **options then dynamically construct a subclass of
138 # admin_class with those **options.
139 if options:
140 # For reasons I don't quite understand, without a
141 # __module__ the created class appears to "live" in the
142 # wrong place, which causes issues later on.
143 options["__module__"] = __name__
144 admin_class = type(
145 "%sAdmin" % model.__name__, (admin_class,), options
146 )
147
148 # Instantiate the admin class to save in the registry
149 self._registry[model] = admin_class(model, self)
150
151 def unregister(self, model_or_iterable):
152 """

Callers 15

readyMethod · 0.45
readyMethod · 0.45
readyMethod · 0.45
register_type_handlersFunction · 0.45
_register_typeMethod · 0.45
readyMethod · 0.45
_model_admin_wrapperFunction · 0.45
readyMethod · 0.45
enableMethod · 0.45

Calls 4

is_registeredMethod · 0.95
get_model_adminMethod · 0.95
AlreadyRegisteredClass · 0.90