MCPcopy
hub / github.com/django/django / Model

Class Model

django/db/models/base.py:501–2512  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

499
500
501class Model(AltersData, metaclass=ModelBase):
502 def __init__(self, *args, **kwargs):
503 # Alias some things as locals to avoid repeat global lookups
504 cls = self.__class__
505 opts = self._meta
506 _setattr = setattr
507 _DEFERRED = DEFERRED
508 if opts.abstract:
509 raise TypeError("Abstract models cannot be instantiated.")
510
511 pre_init.send(sender=cls, args=args, kwargs=kwargs)
512
513 # Set up the storage for instance state
514 self._state = ModelState()
515
516 # There is a rather weird disparity here; if kwargs, it's set, then
517 # args overrides it. It should be one or the other; don't duplicate the
518 # work The reason for the kwargs check is that standard iterator passes
519 # in by args, and instantiation for iteration is 33% faster.
520 if len(args) > len(opts.concrete_fields):
521 # Daft, but matches old exception sans the err msg.
522 raise IndexError("Number of args exceeds number of fields")
523
524 if not kwargs:
525 fields_iter = iter(opts.concrete_fields)
526 # The ordering of the zip calls matter - zip throws StopIteration
527 # when an iter throws it. So if the first iter throws it, the
528 # second is *not* consumed. We rely on this, so don't change the
529 # order without changing the logic.
530 for val, field in zip(args, fields_iter):
531 if val is _DEFERRED:
532 continue
533 _setattr(self, field.attname, val)
534 else:
535 # Slower, kwargs-ready version.
536 fields_iter = iter(opts.fields)
537 for val, field in zip(args, fields_iter):
538 if val is _DEFERRED:
539 continue
540 _setattr(self, field.attname, val)
541 if kwargs.pop(field.name, NOT_PROVIDED) is not NOT_PROVIDED:
542 raise TypeError(
543 f"{cls.__qualname__}() got both positional and "
544 f"keyword arguments for field '{field.name}'."
545 )
546
547 # Now we're left with the unprocessed fields that *must* come from
548 # keywords, or default.
549
550 for field in fields_iter:
551 is_related_object = False
552 # Virtual field
553 if field.column is None or field.generated:
554 continue
555 if kwargs:
556 if isinstance(field.remote_field, ForeignObjectRel):
557 try:
558 # Assume object instance was passed in.

Callers 1

build_instanceFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected