MCPcopy
hub / github.com/django/django / create_many_to_many_intermediary_model

Function create_many_to_many_intermediary_model

django/db/models/fields/related.py:1393–1447  ·  view source on GitHub ↗
(field, klass)

Source from the content-addressed store, hash-verified

1391
1392
1393def create_many_to_many_intermediary_model(field, klass):
1394 from django.db import models
1395
1396 def set_managed(model, related, through):
1397 through._meta.managed = model._meta.managed or related._meta.managed
1398
1399 to_model = resolve_relation(klass, field.remote_field.model)
1400 name = "%s_%s" % (klass._meta.object_name, field.name)
1401 lazy_related_operation(set_managed, klass, to_model, name)
1402
1403 to = make_model_tuple(to_model)[1]
1404 from_ = klass._meta.model_name
1405 if to == from_:
1406 to = "to_%s" % to
1407 from_ = "from_%s" % from_
1408
1409 meta = type(
1410 "Meta",
1411 (),
1412 {
1413 "db_table": field._get_m2m_db_table(klass._meta),
1414 "auto_created": klass,
1415 "app_label": klass._meta.app_label,
1416 "db_tablespace": klass._meta.db_tablespace,
1417 "unique_together": (from_, to),
1418 "verbose_name": _("%(from)s-%(to)s relationship")
1419 % {"from": from_, "to": to},
1420 "verbose_name_plural": _("%(from)s-%(to)s relationships")
1421 % {"from": from_, "to": to},
1422 "apps": field.model._meta.apps,
1423 },
1424 )
1425 # Construct and return the new class.
1426 return type(
1427 name,
1428 (models.Model,),
1429 {
1430 "Meta": meta,
1431 "__module__": klass.__module__,
1432 from_: models.ForeignKey(
1433 klass,
1434 related_name="%s+" % name,
1435 db_tablespace=field.db_tablespace,
1436 db_constraint=field.remote_field.db_constraint,
1437 on_delete=CASCADE,
1438 ),
1439 to: models.ForeignKey(
1440 to_model,
1441 related_name="%s+" % name,
1442 db_tablespace=field.db_tablespace,
1443 db_constraint=field.remote_field.db_constraint,
1444 on_delete=CASCADE,
1445 ),
1446 },
1447 )
1448
1449
1450class ManyToManyField(RelatedField):

Callers 2

contribute_to_classMethod · 0.90
contribute_to_classMethod · 0.85

Calls 4

make_model_tupleFunction · 0.90
lazy_related_operationFunction · 0.85
_get_m2m_db_tableMethod · 0.80
resolve_relationFunction · 0.70

Tested by

no test coverage detected