-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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 QuerySet case in ListSerializer.to_representation method #8632
base: master
Are you sure you want to change the base?
add QuerySet case in ListSerializer.to_representation method #8632
Conversation
f615f16
to
1f9c2b1
Compare
Can you show us a simple example that needs this change? |
all "list" in autogenerate CRUD need this change if in your setting dont have a pagination class setted, comment pagination class and list objects, then remove one and list object again REST_FRAMEWORK = {
# "DEFAULT_PAGINATION_CLASS": "some..."
} |
Okay. Surprised we've not hit this issue before. Thanks for resolving this. |
Looks like this introduces a failing test case... =================================== FAILURES ===================================
[404](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:405)
_____________________ Issue2704TestCase.test_queryset_all ______________________
[405](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:406)
rest_framework/fields.py:447: in get_attribute
[406](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:407)
return get_attribute(instance, self.source_attrs)
[407](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:408)
rest_framework/fields.py:97: in get_attribute
[408](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:409)
instance = getattr(instance, attr)
[409](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:410)
E AttributeError: 'OneFieldModel' object has no attribute 'additional_attr'
[410](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:411)
[411](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:412)
During handling of the above exception, another exception occurred:
[412](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:413)
tests/test_model_serializer.py:1025: in test_queryset_all
[413](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:414)
assert serializer.data == expected
[414](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:415)
rest_framework/serializers.py:768: in data
[415](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:416)
ret = super().data
[416](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:417)
rest_framework/serializers.py:253: in data
[417](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:418)
self._data = self.to_representation(self.instance)
[418](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:419)
rest_framework/serializers.py:687: in to_representation
[419](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:420)
self.child.to_representation(item) for item in iterable
[420](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:421)
rest_framework/serializers.py:687: in <listcomp>
[421](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:422)
self.child.to_representation(item) for item in iterable
[422](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:423)
rest_framework/serializers.py:509: in to_representation
[423](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:424)
attribute = field.get_attribute(instance)
[424](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:425)
rest_framework/fields.py:480: in get_attribute
[425](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:426)
raise type(exc)(msg)
[426](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:427)
E AttributeError: Got AttributeError when attempting to get a value for field `additional_attr` on serializer `TestSerializer`.
[427](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:428)
E The serializer field might be named incorrectly and not match any attribute or key on the `OneFieldModel` instance.
[428](https://github.com/encode/django-rest-framework/runs/8158339360?check_suite_focus=true#step:6:429)
E Original exception text was: 'OneFieldModel' object has no attribute 'additional_attr'. Want to try taking a look into that? If you're not able to resolve what behaviour is being changed there then let me know. |
But tests/test_model_serializer.py:47 class OneFieldModel(models.Model):
char_field = models.CharField(max_length=100) and the error seems to be that problem E AttributeError: Got AttributeError when attempting to get a value for field `additional_attr` on serializer `TestSerializer`.
E The serializer field might be named incorrectly and not match any attribute or key on the `OneFieldModel` instance.
E Original exception text was: 'OneFieldModel' object has no attribute 'additional_attr'. is this needed? |
1f9c2b1
to
d74d206
Compare
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@@ -679,9 +679,18 @@ def to_representation(self, data): | |||
""" | |||
List of object instances -> List of dicts of primitive datatypes. | |||
""" | |||
# Dealing with nested relationships, data can be a Manager, | |||
# so, first get a queryset from the Manager if needed | |||
iterable = data.all() if isinstance(data, models.Manager) else data |
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 are currently having another PR which change instance to basemanager https://github.com/encode/django-rest-framework/pull/8727/files
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 got another PR related to part of this. can you rebase and add a regression test for what you are trying to achieve, please?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Description
QuerySet data type is added in to_representation method because in that case if QuerySet.all() is not called the result iterable is not updated and iterate over an "obsolete list".
Thanks!