1032 | | # Work out the lookup type and remove it from 'parts', if necessary. |
1033 | | if len(parts) == 1 or parts[-1] not in self.query_terms: |
1034 | | lookup_type = 'exact' |
1035 | | else: |
1036 | | lookup_type = parts.pop() |
| 1032 | lookup_type = "exact" |
| 1033 | model = self.model |
| 1034 | # walk the relations and figure out the lookup type |
| 1035 | if len(parts) > 1 and arg not in self.aggregates: |
| 1036 | for idx, part in enumerate(parts): |
| 1037 | try: |
| 1038 | if part == "pk": |
| 1039 | field = model._meta.pk |
| 1040 | else: |
| 1041 | field, _, _, _ = model._meta.get_field_by_name(part) |
| 1042 | if hasattr(field, "model") and field.model != model: |
| 1043 | model = field.model |
| 1044 | continue |
| 1045 | if hasattr(field, "field"): |
| 1046 | # RelatedObjects |
| 1047 | field = field.field |
| 1048 | if field.rel is not None: |
| 1049 | model = field.rel.to |
| 1050 | continue |
| 1051 | # If we reach here we are no longer traversing relations |
| 1052 | if idx < len(parts) - 1 and parts[-1] in self.query_terms: |
| 1053 | lookup_type = parts.pop() |
| 1054 | break |
| 1055 | except FieldDoesNotExist: |
| 1056 | if part in self.aggregates and parts[-1] in self.query_terms: |
| 1057 | lookup_type = parts.pop() |
| 1058 | break |
| 1059 | # if we're not on the last part something is wrong |
| 1060 | if idx != len(parts) - 1: |
| 1061 | # We can't handle this - fall through |
| 1062 | if parts[-1] in self.query_terms: |
| 1063 | lookup_type = parts.pop() |
| 1064 | break |
| 1065 | lookup_type = parts.pop() |
| 1066 | break |