django-rest-framework
django-rest-framework copied to clipboard
add QuerySet case in ListSerializer.to_representation method
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!
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?
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.
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.