MCPcopy
hub / github.com/django/django / _prepare

Method _prepare

django/db/models/options.py:292–334  ·  view source on GitHub ↗
(self, model)

Source from the content-addressed store, hash-verified

290 return pk_class
291
292 def _prepare(self, model):
293 if self.order_with_respect_to:
294 # The app registry will not be ready at this point, so we cannot
295 # use get_field().
296 query = self.order_with_respect_to
297 try:
298 self.order_with_respect_to = next(
299 f
300 for f in self._get_fields(reverse=False)
301 if f.name == query or f.attname == query
302 )
303 except StopIteration:
304 raise FieldDoesNotExist(
305 "%s has no field named '%s'" % (self.object_name, query)
306 )
307
308 self.ordering = ("_order",)
309 if not any(
310 isinstance(field, OrderWrt) for field in model._meta.local_fields
311 ):
312 model.add_to_class("_order", OrderWrt())
313 else:
314 self.order_with_respect_to = None
315
316 if self.pk is None:
317 if self.parents:
318 # Promote the first parent link in lieu of adding yet another
319 # field.
320 field = next(iter(self.parents.values()))
321 # Look for a local field with the same name as the
322 # first parent link. If a local field has already been
323 # created, use it instead of promoting the parent
324 already_created = [
325 fld for fld in self.local_fields if fld.name == field.name
326 ]
327 if already_created:
328 field = already_created[0]
329 field.primary_key = True
330 self.setup_pk(field)
331 else:
332 pk_class = self._get_default_pk_class()
333 auto = pk_class(verbose_name="ID", primary_key=True, auto_created=True)
334 model.add_to_class("id", auto)
335
336 def add_manager(self, manager):
337 self.local_managers.append(manager)

Callers

nothing calls this directly

Calls 7

_get_fieldsMethod · 0.95
setup_pkMethod · 0.95
_get_default_pk_classMethod · 0.95
FieldDoesNotExistClass · 0.90
OrderWrtClass · 0.90
add_to_classMethod · 0.80
valuesMethod · 0.45

Tested by

no test coverage detected