Skip to content

Conversation

timgraham
Copy link
Collaborator

No description provided.

Comment on lines +67 to +74
for model in self.base_field.embedded_models:
with contextlib.suppress(FieldDoesNotExist):
field = model._meta.get_field(name)
break
else:
raise FieldDoesNotExist(
f"The models of field '{self.name}' have no field named '{name}'."
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a test failure caused by moving this logic here:

======================================================================
ERROR: test_nested_lookup (model_fields_.test_polymorphic_embedded_model_array.QueryingTests.test_nested_lookup)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django-mongodb/tests/model_fields_/test_polymorphic_embedded_model_array.py", line 190, in test_nested_lookup
    Owner.objects.filter(pets__toys__name="")
  File "/home/tim/code/django/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/query.py", line 1493, in filter
    return self._filter_or_exclude(False, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/query.py", line 1511, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/home/tim/code/django/django/db/models/query.py", line 1518, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/home/tim/code/django/django/db/models/sql/query.py", line 1646, in add_q
    clause, _ = self._add_q(q_object, can_reuse)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/query.py", line 1678, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/query.py", line 1588, in build_filter
    condition = self.build_lookup(lookups, col, value)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/query.py", line 1409, in build_lookup
    lhs = self.try_transform(lhs, lookup_name)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/sql/query.py", line 1441, in try_transform
    transform_class = lhs.get_transform(name)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django-mongodb/django_mongodb_backend/fields/embedded_model_array.py", line 255, in get_transform
    if transform := self._lhs.get_transform(name):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django/django/db/models/expressions.py", line 406, in get_transform
    return self.output_field.get_transform(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/code/django-mongodb/django_mongodb_backend/fields/polymorphic_embedded_model_array.py", line 70, in get_transform
    raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: The models of field '$item.toys' have no field named 'name'.

It preempts the correct exception from being raised here:

if transform := self._lhs.get_transform(name):
if isinstance(transform, KeyTransformFactory):
raise ValueError("Cannot perform multiple levels of array traversal in a query.")

Copy link
Collaborator

@WaVEV WaVEV Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think it is a feature, haha 😄 . The field name does not exist in Dog's toy. So it is okey if the test raises.
if we define the Bone as follows

class Bone(EmbeddedModel):
    name = models.CharField(max_length=100, null=True)
    brand = models.CharField(max_length=100)

    def __str__(self):
        return self.brand

the test pass

Or the idea was: I will try to filter whatever it is. If the field isn't there it will match None?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The behavior changed of those error messages changed slightly, but I think it's okay. I added additional tests to demonstrate.

@timgraham timgraham force-pushed the simply-embedded-transforms branch from dd451be to 62da08e Compare September 30, 2025 16:08
@timgraham timgraham marked this pull request as ready for review September 30, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants