At least on 2.7, python has no trouble with this:
d={'x-y': 4}
>>> import json
>>> json.dumps(d)
'{"x-y": 4}'
>>>
So that leave's Django's parsing of the order_by string, or just possibly the database connector.
It probably won't work, but you could try:
MyTable.objects.all().order_by("'myfield__en-us'")
(Single quotes inside double quotes, of vice versa.)
It would be instructive if you would post the original error.
There are problems with using internal interfaces, but you might climb down inside the order_by to find something that is called after Django has finished parsing, and substitute the version with the hyphen/minus in arguments below. Of once it has build the query, but before you evaluate it, you could find the order key in the generated SQL (or the string that will be used at generation time if that doesn't happen until evaluation) and edit in the hyphen. I don't promise that there actually are points where you can do either of these things. And whatever you do may break at the next Django version (or the next revision of the database connector.
The better alternative is to figure out where the problem occurs, using code inspection, or by single stepping in with the debugger (PDB directly if you're not using something like PyCharm), then design and test an alternate implementation that is only sensitive to hyphens where required, and propose that code as an enhancement to the Django team (though if the problem is somewhere deeper in the database connector, or heaven forbid, in the DB itself, you may have a harder time finding a sympathetic ear).
Or, at a performance cost, you could skip the order_by clause, and do the sorting in python.
Raw SQL is a possibility.
Good luck.
On Sun, Jan 13, 2019 at 9:11 PM Jason <jjohns98684@gmail.com> wrote:
I think the easiest way would be to convert the dash to an underscore to follow python standards when it comes to naming. reason being, a dash is analogous to the subtraction mathematical operation so you're requiring python to know the difference in the usage of this character in names and operations.--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/607c1021-bde0-4dbd-ac16-415eca8606c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB%2BAj0sFj1M760EPkDc-m0r5q7okqqad--sOaoQ2_e0E-pjhsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment