2015-07-10 15:52:22 +08:00
|
|
|
from django.db import models
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db.models.fields.related import ReverseManyToOneDescriptor
|
2015-07-10 15:52:22 +08:00
|
|
|
from django.db.models.lookups import StartsWith
|
|
|
|
from django.db.models.query_utils import PathInfo
|
|
|
|
|
|
|
|
|
2019-08-20 15:54:41 +08:00
|
|
|
class CustomForeignObjectRel(models.ForeignObjectRel):
|
2015-07-10 15:52:22 +08:00
|
|
|
"""
|
|
|
|
Define some extra Field methods so this Rel acts more like a Field, which
|
2015-09-20 23:51:25 +08:00
|
|
|
lets us use ReverseManyToOneDescriptor in both directions.
|
2015-07-10 15:52:22 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-10 15:52:22 +08:00
|
|
|
@property
|
|
|
|
def foreign_related_fields(self):
|
|
|
|
return tuple(lhs_field for lhs_field, rhs_field in self.field.related_fields)
|
|
|
|
|
|
|
|
def get_attname(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class StartsWithRelation(models.ForeignObject):
|
|
|
|
"""
|
|
|
|
A ForeignObject that uses StartsWith operator in its joins instead of
|
|
|
|
the default equality operator. This is logically a many-to-many relation
|
2015-09-20 23:51:25 +08:00
|
|
|
and creates a ReverseManyToOneDescriptor in both directions.
|
2015-07-10 15:52:22 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-10 15:52:22 +08:00
|
|
|
auto_created = False
|
|
|
|
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = True
|
|
|
|
one_to_many = False
|
|
|
|
one_to_one = False
|
|
|
|
|
|
|
|
rel_class = CustomForeignObjectRel
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs["on_delete"] = models.DO_NOTHING
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2015-07-10 15:52:22 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def field(self):
|
|
|
|
"""
|
2015-09-20 23:51:25 +08:00
|
|
|
Makes ReverseManyToOneDescriptor work in both directions.
|
2015-07-10 15:52:22 +08:00
|
|
|
"""
|
|
|
|
return self.remote_field
|
|
|
|
|
2021-07-21 03:38:17 +08:00
|
|
|
def get_extra_restriction(self, alias, related_alias):
|
2015-07-10 15:52:22 +08:00
|
|
|
to_field = self.remote_field.model._meta.get_field(self.to_fields[0])
|
|
|
|
from_field = self.model._meta.get_field(self.from_fields[0])
|
|
|
|
return StartsWith(to_field.get_col(alias), from_field.get_col(related_alias))
|
|
|
|
|
|
|
|
def get_joining_columns(self, reverse_join=False):
|
2017-06-02 07:08:59 +08:00
|
|
|
return ()
|
2015-07-10 15:52:22 +08:00
|
|
|
|
2017-09-22 23:53:17 +08:00
|
|
|
def get_path_info(self, filtered_relation=None):
|
2015-07-10 15:52:22 +08:00
|
|
|
to_opts = self.remote_field.model._meta
|
|
|
|
from_opts = self.model._meta
|
2017-09-22 23:53:17 +08:00
|
|
|
return [
|
|
|
|
PathInfo(
|
|
|
|
from_opts=from_opts,
|
|
|
|
to_opts=to_opts,
|
|
|
|
target_fields=(to_opts.pk,),
|
|
|
|
join_field=self,
|
|
|
|
m2m=False,
|
|
|
|
direct=False,
|
|
|
|
filtered_relation=filtered_relation,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_reverse_path_info(self, filtered_relation=None):
|
2015-07-10 15:52:22 +08:00
|
|
|
to_opts = self.model._meta
|
|
|
|
from_opts = self.remote_field.model._meta
|
2017-09-22 23:53:17 +08:00
|
|
|
return [
|
|
|
|
PathInfo(
|
|
|
|
from_opts=from_opts,
|
|
|
|
to_opts=to_opts,
|
|
|
|
target_fields=(to_opts.pk,),
|
|
|
|
join_field=self.remote_field,
|
|
|
|
m2m=False,
|
|
|
|
direct=False,
|
|
|
|
filtered_relation=filtered_relation,
|
|
|
|
)
|
|
|
|
]
|
2015-07-10 15:52:22 +08:00
|
|
|
|
2016-03-21 01:10:55 +08:00
|
|
|
def contribute_to_class(self, cls, name, private_only=False):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().contribute_to_class(cls, name, private_only)
|
2015-09-20 23:51:25 +08:00
|
|
|
setattr(cls, self.name, ReverseManyToOneDescriptor(self))
|
2015-07-10 15:52:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BrokenContainsRelation(StartsWithRelation):
|
|
|
|
"""
|
|
|
|
This model is designed to yield no join conditions and
|
|
|
|
raise an exception in ``Join.as_sql()``.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-07-21 03:38:17 +08:00
|
|
|
def get_extra_restriction(self, alias, related_alias):
|
2015-07-10 15:52:22 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class SlugPage(models.Model):
|
2016-03-18 22:24:29 +08:00
|
|
|
slug = models.CharField(max_length=20, unique=True)
|
2015-07-10 15:52:22 +08:00
|
|
|
descendants = StartsWithRelation(
|
|
|
|
"self",
|
|
|
|
from_fields=["slug"],
|
|
|
|
to_fields=["slug"],
|
|
|
|
related_name="ascendants",
|
|
|
|
)
|
|
|
|
containers = BrokenContainsRelation(
|
|
|
|
"self",
|
|
|
|
from_fields=["slug"],
|
|
|
|
to_fields=["slug"],
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["slug"]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "SlugPage %s" % self.slug
|