MCPcopy
hub / github.com/django/django / _check_relationship_model

Method _check_relationship_model

django/db/models/fields/related.py:1589–1842  ·  view source on GitHub ↗
(self, from_model=None, **kwargs)

Source from the content-addressed store, hash-verified

1587 return warnings
1588
1589 def _check_relationship_model(self, from_model=None, **kwargs):
1590 from django.db.models.fields.composite import CompositePrimaryKey
1591
1592 if hasattr(self.remote_field.through, "_meta"):
1593 qualified_model_name = "%s.%s" % (
1594 self.remote_field.through._meta.app_label,
1595 self.remote_field.through.__name__,
1596 )
1597 else:
1598 qualified_model_name = self.remote_field.through
1599
1600 errors = []
1601
1602 if self.remote_field.through not in self.opts.apps.get_models(
1603 include_auto_created=True
1604 ):
1605 # The relationship model is not installed.
1606 errors.append(
1607 checks.Error(
1608 "Field specifies a many-to-many relation through model "
1609 "'%s', which has not been installed." % qualified_model_name,
1610 obj=self,
1611 id="fields.E331",
1612 )
1613 )
1614
1615 else:
1616 assert from_model is not None, (
1617 "ManyToManyField with intermediate "
1618 "tables cannot be checked if you don't pass the model "
1619 "where the field is attached to."
1620 )
1621 # Set some useful local variables
1622 to_model = resolve_relation(from_model, self.remote_field.model)
1623 from_model_name = from_model._meta.object_name
1624 if isinstance(to_model, str):
1625 to_model_name = to_model
1626 else:
1627 to_model_name = to_model._meta.object_name
1628 if self.remote_field.through_fields is None and not isinstance(
1629 to_model, str
1630 ):
1631 model_name = None
1632 if isinstance(to_model._meta.pk, CompositePrimaryKey):
1633 model_name = self.remote_field.model._meta.object_name
1634 elif isinstance(from_model._meta.pk, CompositePrimaryKey):
1635 model_name = from_model_name
1636 if model_name:
1637 errors.append(
1638 checks.Error(
1639 f"Field defines a relation involving model {model_name!r} "
1640 "which has a CompositePrimaryKey and such relations are "
1641 "not supported.",
1642 obj=self,
1643 id="fields.E347",
1644 )
1645 )
1646 relationship_model_name = self.remote_field.through._meta.object_name

Callers 1

checkMethod · 0.95

Calls 5

resolve_relationFunction · 0.70
get_modelsMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45
get_fieldMethod · 0.45

Tested by

no test coverage detected