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
Fixed #26511 -- Documented KeyTransform and KeyTextTransform #15956
Conversation
Looks to me that there is no use in using |
That's not true, see e.g. comment. |
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.
@AllenJonathan Thanks 👍 I left initial comments.
docs/topics/db/queries.txt
Outdated
@@ -1009,6 +1009,63 @@ Unless you are sure you wish to work with SQL ``NULL`` values, consider setting | |||
Key, index, and path transforms | |||
------------------------------- | |||
|
|||
.. class:: KeyTransform(key_name, *args, **kwargs) |
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.
.. class:: KeyTransform(key_name, *args, **kwargs) | |
.. class:: json.KeyTransform(key_name, *args, **kwargs) |
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 would move it to the end this paragraph (before the closing notes, warnings, and admonitions).
docs/topics/db/queries.txt
Outdated
in ``'"labrador"'`` instead of ``'labrador'``. So filtering | ||
``breed_name__contains='dor"'`` would also be true. | ||
|
||
.. class:: KeyTextTransform |
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 would move it to the end this paragraph (before the closing notes, warnings, and admonitions). Also, add the full signature.
docs/topics/db/queries.txt
Outdated
in ``'"labrador"'`` instead of ``'labrador'``. So filtering | ||
``breed_name__contains='dor"'`` would also be true. | ||
|
||
.. class:: KeyTextTransform |
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.
.. class:: KeyTextTransform | |
.. class:: json.KeyTextTransform(key_name, *args, **kwargs) |
docs/topics/db/queries.txt
Outdated
.. note:: | ||
|
||
This feature is currently supported only in postgresql databases. |
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.
We should rather document that on non-PostgreSQL backends it's and alias to KeyTransform
.
docs/topics/db/queries.txt
Outdated
Casting a resulting KeyTransform might still be in resemble json format. | ||
For example, ``Cast(KeyTransform('breed', 'data'), TextField())`` would result | ||
in ``'"labrador"'`` instead of ``'labrador'``. So filtering | ||
``breed_name__contains='dor"'`` would also be true. |
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 is not true for all databases.
docs/topics/db/queries.txt
Outdated
|
||
.. warning:: | ||
|
||
Casting a resulting KeyTransform might still be in resemble json format. |
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.
KeyTransform
should be backticked everywhere.
docs/topics/db/queries.txt
Outdated
... breed_name=Cast(KeyTransform('breed', 'data'), TextField()) | ||
... ).filter(breed_name__contains='lab') |
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.
Fix indentation (everywhere):
... breed_name=Cast(KeyTransform('breed', 'data'), TextField()) | |
... ).filter(breed_name__contains='lab') | |
... breed_name=Cast(KeyTransform('breed', 'data'), TextField()) | |
... ).filter(breed_name__contains='lab') |
docs/topics/db/queries.txt
Outdated
... )).filter(owner_other_pet_name__exact=[{'name': 'Fishy'}]) | ||
<QuerySet [<Dog: Rufus>]> | ||
|
||
To query the resulting json as text, the .. class:: Cast function needs to be |
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.
.. class:: Cast
is not a valid directive in Sphinx.
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.
Please fix all invalid directives, e.g. .. class:: json.KeyTextTransform
or .. class:: json.KeyTransform
.
Is there a way to run all the doc tests locally? |
You can use |
docs/topics/db/queries.txt
Outdated
To query the resulting json as text, the :class:`~django.db.models.functions.Cast` | ||
function needs to be used. This is done as follows:: | ||
|
||
>>> Dogs.objects.annotate( | ||
... breed_name=Cast(KeyTransform('breed', 'data'), TextField()) | ||
... ).filter(breed_name__contains='lab') | ||
<QuerySet [<Dog: Rufus>]> |
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 this is bad advice as it will result in quotes around the string being included? Only KeyTextTransform
should be used for that really.
docs/topics/db/queries.txt
Outdated
.. note:: | ||
|
||
.. class:: json.KeyTextTransform is alias to .. class:: json.KeyTransform for | ||
non-postgresql databases. |
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.
Is this an oversight? I'm surprised to learn that.
Should we make sure that KeyTextTransform
actually results in text
(or whatever database specific type is used to represent strings) before making it public?
It seems the ->>
operator is supported on MySQL 5.7+ and on other backend we could make sure that the return value is automatically wrapped in a CAST
(e.g. a CAST
over json_value
on Oracle)
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.
It works on SQLite and Oracle as a side-effect of the ticket-32213. I've prepared a fix for MySQL, see #15970.
Should the earlier ways to do path, index, and key transforms on |
Last minute check but should we consider changing the signature of If this is not possible for backward compatibility reasons should we consider exposing aliases à la What about class KeyTransform(Transform):
...
@classmethod
def from_lookup(cls, lookup):
transform, *keys = lookup.split(LOOKUP_SEP)
if not keys:
raise ValueError("Missing keys")
for key in keys:
transform = cls(key, transform)
return transform
K = KeyTransform.from_lookup
KT = KeyTextTransform.from_lookup It seems like these e.g. to take the documented examples Dogs.objects.annotate(
owner_name=KT('data__owner__name')
).filter(owner_name="Bob") |
Actually it seems like The usefulness of Any thoughts @AllenJonathan and @felixxm? Allen, can you confirm that using |
Yes, I could do the above changes. Should that be a new ticket and a PR? I think this ticket could be put on hold till these changes are made. |
Agreed, Any thoughts on the subject @felixxm? |
Agreed, let's add and document only the alias Also, we recently documented |
|
Final Work Submission - Link This is going to be my final work submission for GSOC. Please go through it and suggest changes or additions if any. |
@AllenJonathan Thanks 👍 Please move docs changes to the #16016, we should add and documented |
No description provided.