|
1 | | -import functools |
2 | | -import inspect |
3 | 1 | from functools import cached_property, partial |
4 | 2 |
|
5 | 3 | from plain import exceptions, preflight |
|
27 | 25 | RelatedLessThan, |
28 | 26 | RelatedLessThanOrEqual, |
29 | 27 | ) |
30 | | -from .reverse_related import ForeignObjectRel, ManyToManyRel, ManyToOneRel |
| 28 | +from .reverse_related import ManyToManyRel, ManyToOneRel |
31 | 29 |
|
32 | 30 | RECURSIVE_RELATIONSHIP_CONSTANT = "self" |
33 | 31 |
|
@@ -367,18 +365,12 @@ def get_reverse_related_filter(self, obj): |
367 | 365 | select all instances of self.related_field.model related through |
368 | 366 | this field to obj. obj is an instance of self.model. |
369 | 367 | """ |
370 | | - base_q = Q.create( |
| 368 | + return Q.create( |
371 | 369 | [ |
372 | 370 | (rh_field.attname, getattr(obj, lh_field.attname)) |
373 | 371 | for lh_field, rh_field in self.related_fields |
374 | 372 | ] |
375 | 373 | ) |
376 | | - descriptor_filter = self.get_extra_descriptor_filter(obj) |
377 | | - if isinstance(descriptor_filter, dict): |
378 | | - return base_q & Q(**descriptor_filter) |
379 | | - elif descriptor_filter: |
380 | | - return base_q & descriptor_filter |
381 | | - return base_q |
382 | 374 |
|
383 | 375 | def set_attributes_from_rel(self): |
384 | 376 | self.name = self.name or (self.remote_field.model._meta.model_name + "_" + "id") |
@@ -437,16 +429,13 @@ class ForeignKey(RelatedField): |
437 | 429 | """ |
438 | 430 |
|
439 | 431 | descriptor_class = ForeignKeyDeferredAttribute |
440 | | - |
441 | | - # Field flags |
442 | | - many_to_many = False |
443 | | - many_to_one = True |
444 | | - one_to_many = False |
445 | | - |
446 | 432 | related_accessor_class = ReverseManyToOneDescriptor |
447 | 433 | forward_related_accessor_class = ForwardManyToOneDescriptor |
448 | 434 | rel_class = ManyToOneRel |
449 | 435 |
|
| 436 | + # Field flags - ForeignKey is many-to-one |
| 437 | + many_to_one = True |
| 438 | + |
450 | 439 | empty_strings_allowed = False |
451 | 440 | default_error_messages = { |
452 | 441 | "invalid": "%(model)s instance with %(field)s %(value)r does not exist." |
@@ -494,9 +483,6 @@ def __init__( |
494 | 483 | self.db_index = db_index |
495 | 484 | self.db_constraint = db_constraint |
496 | 485 |
|
497 | | - def __class_getitem__(cls, *args, **kwargs): |
498 | | - return cls |
499 | | - |
500 | 486 | def __copy__(self): |
501 | 487 | obj = super().__copy__() |
502 | 488 | # Remove any cached PathInfo values. |
@@ -545,21 +531,6 @@ def get_joining_columns(self, reverse_join=False): |
545 | 531 | def get_reverse_joining_columns(self): |
546 | 532 | return self.get_joining_columns(reverse_join=True) |
547 | 533 |
|
548 | | - def get_extra_descriptor_filter(self, instance): |
549 | | - """ |
550 | | - Return an extra filter condition for related object fetching when |
551 | | - user does 'instance.fieldname', that is the extra filter is used in |
552 | | - the descriptor of the field. |
553 | | -
|
554 | | - The filter should be either a dict usable in .filter(**kwargs) call or |
555 | | - a Q-object. The condition will be ANDed together with the relation's |
556 | | - joining columns. |
557 | | -
|
558 | | - A parallel method is get_extra_restriction() which is used in |
559 | | - JOIN and subquery conditions. |
560 | | - """ |
561 | | - return {} |
562 | | - |
563 | 534 | def get_extra_restriction(self, alias, related_alias): |
564 | 535 | """ |
565 | 536 | Return a pair condition used for joining and subquery pushdown. The |
@@ -614,15 +585,6 @@ def get_reverse_path_info(self, filtered_relation=None): |
614 | 585 | def reverse_path_infos(self): |
615 | 586 | return self.get_reverse_path_info() |
616 | 587 |
|
617 | | - @classmethod |
618 | | - @functools.cache |
619 | | - def get_class_lookups(cls): |
620 | | - bases = inspect.getmro(cls) |
621 | | - # Find ForeignKey in the MRO instead of ForeignObject |
622 | | - bases = bases[: bases.index(ForeignKey) + 1] |
623 | | - class_lookups = [parent.__dict__.get("class_lookups", {}) for parent in bases] |
624 | | - return cls.merge_dicts(class_lookups) |
625 | | - |
626 | 588 | def contribute_to_class(self, cls, name, private_only=False, **kwargs): |
627 | 589 | super().contribute_to_class(cls, name, private_only=private_only, **kwargs) |
628 | 590 | setattr(cls, self.name, self.forward_related_accessor_class(self)) |
@@ -792,15 +754,6 @@ def db_parameters(self, connection): |
792 | 754 | "collation": target_db_parameters.get("collation"), |
793 | 755 | } |
794 | 756 |
|
795 | | - def convert_empty_strings(self, value, expression, connection): |
796 | | - if (not value) and isinstance(value, str): |
797 | | - return None |
798 | | - return value |
799 | | - |
800 | | - def get_db_converters(self, connection): |
801 | | - converters = super().get_db_converters(connection) |
802 | | - return converters |
803 | | - |
804 | 757 | def get_col(self, alias, output_field=None): |
805 | 758 | if output_field is None: |
806 | 759 | output_field = self.target_field |
|
0 commit comments