Skip to content
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

add field name validation to EmbeddedModelField lookups #224

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Jan 18, 2025

fixes #217

@WaVEV WaVEV force-pushed the EMF-field-name-validation branch 3 times, most recently from 483c876 to f4f229a Compare January 18, 2025 05:05
@WaVEV WaVEV changed the title field name validation EMF field name validation Jan 18, 2025
@@ -104,6 +104,16 @@ def test_nested(self):
)
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])

def test_nested_not_exists(self):
msg = "Address.city has no field named 'president'"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error doesn't look correct. city is a CharField which doesn't have subfields (rather than address being model in the other test) . Instead what would come after it would be a lookup. I'd expect an error like: FieldError: Unsupported lookup 'president' for CharField 'city'. If that's feasible, can we also use the "perhaps you meant" logic from https://github.com/django/django/blob/c28f821c9067050ba0d099349a4dfea2b29faf99/django/db/models/sql/query.py#L1436-L1452?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤔 I think is possible, will try.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you try adding the "perhaps you meant" logic to suggest what lookup the user may have mistyped?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't, but I think it is possible, just have to check the choices like Django does (don't remember the method that does this)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done 😄 , I never read more than 3 lines.

tests/model_fields_/test_embedded_model.py Show resolved Hide resolved
Jibola
Jibola previously requested changes Jan 21, 2025
django_mongodb_backend/fields/embedded_model.py Outdated Show resolved Hide resolved
@timgraham timgraham changed the title EMF field name validation add field name validation to EmbeddedModelField lookups Jan 23, 2025
@timgraham timgraham force-pushed the EMF-field-name-validation branch from 5189e55 to a1c6b39 Compare January 23, 2025 02:01
@timgraham timgraham dismissed Jibola’s stale review January 23, 2025 02:28

comment added!

@timgraham timgraham force-pushed the EMF-field-name-validation branch from a1c6b39 to 37af20e Compare January 23, 2025 02:50
Jibola
Jibola previously requested changes Jan 23, 2025
Copy link
Collaborator

@Jibola Jibola left a comment

Choose a reason for hiding this comment

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

One addition. Everything else looks great.

Comment on lines +166 to +182
def test_invalid_lookup_with_suggestions(self):
msg = (
"Unsupported lookup '{lookup}' for CharField 'name', "
"perhaps you meant {suggested_lookups}?"
)
with self.assertRaisesMessage(
FieldDoesNotExist, msg.format(lookup="exactly", suggested_lookups="exact or iexact")
):
Book.objects.filter(author__name__exactly="NYC")
with self.assertRaisesMessage(
FieldDoesNotExist, msg.format(lookup="gti", suggested_lookups="gt or gte")
):
Book.objects.filter(author__name__gti="NYC")
with self.assertRaisesMessage(
FieldDoesNotExist, msg.format(lookup="is_null", suggested_lookups="isnull")
):
Book.objects.filter(author__name__is_null="NYC")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a test that also checks the case where no suggestion will be provided?

with self.assertRaisesMessage(
            FieldDoesNotExist, "Unsupported lookup '{lookup}' for CharField 'name'."
        ):
                Book.objects.filter(author__name__zzzz="NYC")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The test is on the line 161, test_invalid_lookup

@timgraham timgraham dismissed Jibola’s stale review January 23, 2025 14:52

test already there

@timgraham timgraham merged commit 37af20e into mongodb-labs:main Jan 23, 2025
9 checks passed
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.

add field name validation to EmbeddedModelField lookups
3 participants