2017-09-07 01:11:18 +08:00
|
|
|
from functools import partial
|
|
|
|
|
2015-05-24 18:16:21 +08:00
|
|
|
from django.db import models
|
2015-01-09 06:51:00 +08:00
|
|
|
from django.db.models.fields.related import (
|
2019-08-20 15:54:41 +08:00
|
|
|
RECURSIVE_RELATIONSHIP_CONSTANT,
|
|
|
|
ManyToManyDescriptor,
|
|
|
|
RelatedField,
|
|
|
|
create_many_to_many_intermediary_model,
|
2015-01-09 06:51:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomManyToManyField(RelatedField):
|
|
|
|
"""
|
|
|
|
Ticket #24104 - Need to have a custom ManyToManyField,
|
|
|
|
which is not an inheritor of ManyToManyField.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-01-09 06:51:00 +08:00
|
|
|
many_to_many = True
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
to,
|
|
|
|
db_constraint=True,
|
|
|
|
swappable=True,
|
|
|
|
related_name=None,
|
|
|
|
related_query_name=None,
|
|
|
|
limit_choices_to=None,
|
|
|
|
symmetrical=None,
|
|
|
|
through=None,
|
|
|
|
through_fields=None,
|
|
|
|
db_table=None,
|
|
|
|
**kwargs,
|
|
|
|
):
|
2015-01-09 06:51:00 +08:00
|
|
|
try:
|
|
|
|
to._meta
|
|
|
|
except AttributeError:
|
|
|
|
to = str(to)
|
2019-08-20 15:54:41 +08:00
|
|
|
kwargs["rel"] = models.ManyToManyRel(
|
2015-01-09 06:51:00 +08:00
|
|
|
self,
|
|
|
|
to,
|
2017-02-02 00:41:56 +08:00
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
symmetrical=symmetrical
|
|
|
|
if symmetrical is not None
|
|
|
|
else (to == RECURSIVE_RELATIONSHIP_CONSTANT),
|
|
|
|
through=through,
|
|
|
|
through_fields=through_fields,
|
2015-01-09 06:51:00 +08:00
|
|
|
db_constraint=db_constraint,
|
|
|
|
)
|
|
|
|
self.swappable = swappable
|
2017-02-02 00:41:56 +08:00
|
|
|
self.db_table = db_table
|
2021-03-24 13:45:08 +08:00
|
|
|
if kwargs["rel"].through is not None and self.db_table is not None:
|
|
|
|
raise ValueError(
|
|
|
|
"Cannot specify a db_table if an intermediary model is used."
|
|
|
|
)
|
2021-04-29 03:15:26 +08:00
|
|
|
super().__init__(
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2015-01-09 06:51:00 +08:00
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, **kwargs):
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.symmetrical and (
|
|
|
|
self.remote_field.model == "self"
|
|
|
|
or self.remote_field.model == cls._meta.object_name
|
|
|
|
):
|
|
|
|
self.remote_field.related_name = "%s_rel_+" % name
|
2017-01-21 21:13:44 +08:00
|
|
|
super().contribute_to_class(cls, name, **kwargs)
|
2015-02-26 22:19:17 +08:00
|
|
|
if (
|
|
|
|
not self.remote_field.through
|
|
|
|
and not cls._meta.abstract
|
|
|
|
and not cls._meta.swapped
|
|
|
|
):
|
|
|
|
self.remote_field.through = create_many_to_many_intermediary_model(
|
|
|
|
self, cls
|
|
|
|
)
|
2015-09-20 23:51:25 +08:00
|
|
|
setattr(cls, self.name, ManyToManyDescriptor(self.remote_field))
|
2017-09-07 01:11:18 +08:00
|
|
|
self.m2m_db_table = partial(self._get_m2m_db_table, cls._meta)
|
2015-01-09 06:51:00 +08:00
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return "ManyToManyField"
|
|
|
|
|
|
|
|
# Copy those methods from ManyToManyField because they don't call super() internally
|
2019-08-20 15:54:41 +08:00
|
|
|
contribute_to_related_class = models.ManyToManyField.__dict__[
|
|
|
|
"contribute_to_related_class"
|
|
|
|
]
|
|
|
|
_get_m2m_attr = models.ManyToManyField.__dict__["_get_m2m_attr"]
|
|
|
|
_get_m2m_reverse_attr = models.ManyToManyField.__dict__["_get_m2m_reverse_attr"]
|
|
|
|
_get_m2m_db_table = models.ManyToManyField.__dict__["_get_m2m_db_table"]
|
2015-01-29 22:14:55 +08:00
|
|
|
|
|
|
|
|
2019-08-20 15:54:41 +08:00
|
|
|
class InheritedManyToManyField(models.ManyToManyField):
|
2015-01-29 22:14:55 +08:00
|
|
|
pass
|
2015-05-24 18:16:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MediumBlobField(models.BinaryField):
|
|
|
|
"""
|
|
|
|
A MySQL BinaryField that uses a different blob size.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-24 18:16:21 +08:00
|
|
|
def db_type(self, connection):
|
|
|
|
return "MEDIUMBLOB"
|