Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lsaavedr
Copy link

@lsaavedr lsaavedr commented Sep 1, 2022

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!

@lsaavedr lsaavedr force-pushed the fix_ListSerializer.to_representation branch 2 times, most recently from f615f16 to 1f9c2b1 Compare September 2, 2022 12:41
@tomchristie
Copy link
Member

Can you show us a simple example that needs this change?

@lsaavedr
Copy link
Author

lsaavedr commented Sep 2, 2022

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..."
 }

@tomchristie
Copy link
Member

Okay. Surprised we've not hit this issue before. Thanks for resolving this.

@tomchristie
Copy link
Member

tomchristie commented Sep 2, 2022

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.

@lsaavedr
Copy link
Author

lsaavedr commented Sep 2, 2022

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?

@lsaavedr lsaavedr force-pushed the fix_ListSerializer.to_representation branch from 1f9c2b1 to d74d206 Compare September 8, 2022 14:52
@stale
Copy link

stale bot commented Nov 12, 2022

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.

@stale stale bot added the stale label Nov 12, 2022
@@ -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
Copy link
Member

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

@stale stale bot removed stale labels Nov 22, 2022
Copy link
Member

@auvipy auvipy left a 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?

@stale
Copy link

stale bot commented Jan 22, 2023

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.

@stale stale bot added the stale label Jan 22, 2023
@auvipy auvipy removed the stale label Jan 22, 2023
@stale
Copy link

stale bot commented Mar 25, 2023

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.

@stale stale bot added the stale label Mar 25, 2023
@auvipy auvipy removed the stale label Mar 25, 2023
@stale
Copy link

stale bot commented Jun 18, 2023

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.

@stale stale bot added the stale label Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants