-
Notifications
You must be signed in to change notification settings - Fork 29
Simplify EmbeddedModelField transform classes #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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}'." | ||
) |
There was a problem hiding this comment.
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:
django-mongodb-backend/django_mongodb_backend/fields/embedded_model_array.py
Lines 256 to 258 in e747973
if transform := self._lhs.get_transform(name): | |
if isinstance(transform, KeyTransformFactory): | |
raise ValueError("Cannot perform multiple levels of array traversal in a query.") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
dd451be
to
62da08e
Compare
No description provided.