-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
483c876
to
f4f229a
Compare
@@ -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'" |
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.
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?
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.
🤔 I think is possible, will try.
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.
Done
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.
Did you try adding the "perhaps you meant" logic to suggest what lookup the user may have mistyped?
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.
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)
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.
Done 😄 , I never read more than 3 lines.
5189e55
to
a1c6b39
Compare
a1c6b39
to
37af20e
Compare
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.
One addition. Everything else looks great.
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") |
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.
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")
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.
The test is on the line 161, test_invalid_lookup
fixes #217