-
Notifications
You must be signed in to change notification settings - Fork 208
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
GET method does not return selected choices with Django REST Framework #73
Comments
Are you using the
That would be great! |
I found additional weirdness with the GET method. First here's how to replicate the issues. git clone https://github.com/stevepiercy/rest-framework-tutorial.git
cd rest-framework-tutorial
git checkout issue-73-multiple-choice-field
python3 -m venv env
source env/bin/activate
pip install --upgrade pip setuptools
pip install django djangorestframework pygments django-multiselectfield coreapi
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
# ignore warning:
# WARNINGS:
# ?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting,without specifying also a DEFAULT_PAGINATION_CLASS.
# HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was # PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.
# follow prompts
python manage.py runserver In a web browser, visit http://127.0.0.1:8000/snippets/1/ Select all values for Multiselect field. Click PUT. Finally click the GET button. Instead of all values being selected, only the values False, True, Two, and Three are selected. The weirdness manifests when the choices are not Let me know if you need further information to reproduce. I'm not sure whether I'm doing choices right, or whether this is in fact a bug either in code or documentation somewhere in DRF, Django, or this package. Thanks for your help. |
Sorry, I didn't mean to ignore you for two months. I've been busier than normal lately. I managed to reproduce your issue once. You are correct - the data is saved in the database as: DRF emits the JSON as: {
"url": "http://127.0.0.1:8000/snippets/1/",
"id": 1,
"highlight": "http://127.0.0.1:8000/snippets/1/highlight/",
"owner": "blag",
"title": "asdf",
"code": "fdsa",
"linenos": "True",
"language": "abap",
"style": "abap",
"multiselect": [
0,
1,
2,
",",
3,
".",
"C",
"}",
"W",
"O",
" ",
"5",
"{",
"'",
"R",
"A"
]
} Unforunately, every time I tried to reproduce it after that it worked just fine. DRF emitted this (correct) JSON: {
"url": "http://127.0.0.1:8000/snippets/1/",
"id": 1,
"highlight": "http://127.0.0.1:8000/snippets/1/highlight/",
"owner": "blag",
"title": "adsfasdfasdf",
"code": "fdsa",
"linenos": "True",
"language": "abap",
"style": "abap",
"multiselect": [
0,
1,
2,
3,
3.5,
10,
"WA",
"OR",
"CA"
]
} If you can reliably reproduce this bug, please try to troubleshoot it a bit more. You can manually "install" django-multiselectfield and django-rest-framework by cloning them into your project repository with git and symlinking the appropriate directories, then not installing them with git clone https://github.com/stevepiercy/rest-framework-tutorial.git
cd rest-framework-tutorial
# vvvv
git clone https://github.com/goinnn/django-multiselectfield.git
ln -s django-multiselectfield/multiselectfield multiselectfield
git clone https://github.com/encode/django-rest-framework.git
ln -s django-rest-framework/rest_framework rest_framework
# ^^^^
git checkout issue-73-multiple-choice-field
python3 -m venv env
source env/bin/activate
pip install --upgrade pip setuptools
# DON'T install django-multiselectfield or django-rest-framework here
pip install django pygments coreapi
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
# ignore warning:
# WARNINGS:
# ?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting,without specifying also a DEFAULT_PAGINATION_CLASS.
# HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was # PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.
# follow prompts
python manage.py runserver That should allow you to play with django-multiselectfield or DRF to see if that's causing the issue. It's a little kludgy, but you can sprinkle Please keep in mind that this project is kind of a hack similar to Django's Let me know if you run into any issues troubleshooting and I'll do what I can to help you. Thanks! |
After following your installation instructions, I've been able to reliably produce the issue, just like with my original issue report. Can you suggest where I ought to put some print statements? I think the first place to look would be wherever DRF makes a query to the database and to view the result. Then step through how DRF parses that result and converts it to JSON. FTR, I created two snippets, then did a GET for them, which returned the following JSON. HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"url": "http://127.0.0.1:8000/snippets/1/",
"id": 1,
"highlight": "http://127.0.0.1:8000/snippets/1/highlight/",
"owner": "stevepiercy",
"title": "title",
"code": "print('hello world')",
"linenos": "True",
"language": "python3",
"style": "default",
"multiselect": [
0,
1,
2,
"{",
3,
"5",
"C",
",",
"'",
".",
" ",
"W",
"O",
"R",
"A",
"}"
]
},
{
"url": "http://127.0.0.1:8000/snippets/2/",
"id": 2,
"highlight": "http://127.0.0.1:8000/snippets/2/highlight/",
"owner": "stevepiercy",
"title": "title",
"code": "print('hello world')",
"linenos": "True",
"language": "python3",
"style": "default",
"multiselect": [
0,
1,
2,
"{",
3,
"5",
"C",
",",
"'",
".",
" ",
"W",
"O",
"R",
"A",
"}"
]
}
] |
Hm, that's interesting. I have more questions for you then.
I assume you're using a virtualenv, so find the |
Shoot. I didn't pin Django, so I had 2.0.1 installed. I fixed that by uninstalling, then reinstalling 1.11.9. Here are my current versions:
DRF is symlinked to the cloned repo as you previously instructed. So is multiselectfield. Nonetheless, now with these versions, I still experience the issue. When I do a There is no What should I try next? def to_representation(self, value):
for item in value:
print('to_representation - value ' + str(
self.choice_strings_to_values.get(six.text_type(item), item)))
return {
self.choice_strings_to_values.get(six.text_type(item), item) for item in value
}
|
I've also tried Django's |
@stevepiercy Thanks for the additional information! The printed data there indicates that I suspect that this isn't going to be solved quickly, and it looks like you will someday try to filter on the I think the appropriate relationship is a proper M2M for you. Not only will it be less buggy, it will give you control over how to display those relations to the user, and be well supported by Django. Unfortunately there aren't super easy ways to handle that with DRF. It does support this though - look into the DRF documentation on nested relationships and either nested routes or nested routers. |
@blag thank you. The M2M relationship option is what I am pursuing. I haven't figured out whether DRF can render such relationships in its web UI, and if it can, where I can find such an example that is complete. Stack Overflow examples lack completeness. |
I am curious to know if you ever managed to find a fix for this? |
Yup, I went with Pyramid, SQLAlchemy, and Deform. Probably not the answer you wanted, but it gives me the control I need. |
In Django REST Framework, I noticed that PUT, PATCH, and OPTIONS methods work just fine. But when I GET an instance, I receive a list of values that are single characters. I expected to receive a list of the previously PUT or PATCH'ed choices.
I also looked in the database, and the values in there are exactly what was PUT or PATCH'ed as comma-separated values.
I'm not sure where to begin to troubleshoot, but at least I wanted to see if anyone else had this issue. It might be in my own code.
I can make a reproducible example using the DRF tutorial application as the base, if that would help.
See also #55.
The text was updated successfully, but these errors were encountered: