django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

use_pk_only_optimization method on PrimaryKeyRelatedField is not documented

Open staticmetal opened this issue 9 years ago • 7 comments

PrimaryKeyRelatedField does not work properly if you don't use a specific queryset.

for example:

my_field = PrimaryKeyRelatedField(queryset=MyModel.objects.all())

If I use the above field it will give me json serialization error in response. Sinceuse_pk_only_optimization is set to true and the field assumes that the objects returned by the queryset are pk objects. But instead they are full objects.

While this will work fine:

my_field = PrimaryKeyRelatedField(queryset=MyModel.objects.all().values_list('pk', flat=True))

This behaviour of PrimaryKeyRelatedField field is undocumented on page [1]. It would really help a lot if this behaviour is explicitly mentioned and users know which queryset to use.

MyModel.objects.all() will work fine for write_only cases but not for read and write both.

[1] http://www.django-rest-framework.org/api-guide/relations/?q=use_pk_only_optimization#primarykeyrelatedfield

staticmetal avatar Jun 29 '17 16:06 staticmetal

https://github.com/encode/django-rest-framework/blob/master/tests/test_relations.py#L110 test case should already cover this use case.

xordoquy avatar Jun 30 '17 06:06 xordoquy

If use_pk_only_optimization is set the true the instance is of type PKOnlyObject and the attribute pk on the instance can be the whole object. I think this behaviour needs to be documented. The test should check if the type of pk matches the type of the pk field on the model.

staticmetal avatar Jun 30 '17 07:06 staticmetal

Adding use_pk_only_optimization to the documentation means it won't be a private part any longer and will be harder to change and we want to avoid that.

xordoquy avatar Jul 04 '17 08:07 xordoquy

Sure, I get that. When we need is to mention the queryset for PrimaryKeyRelatedField for supporting serialization of data in GET requests. The queryset should be model.objects.all().values_list('pk', flat=True). This queryset supports read and write both. While querysets which return whole objects like model.objects.all() cannot be used for GET requests.

staticmetal avatar Jul 04 '17 09:07 staticmetal

I'm fine with that. Would appreciate another opinion here.

xordoquy avatar Jul 04 '17 09:07 xordoquy

MyModel.objects.all() will work fine for write_only cases but not for read and write both.

Can you expand on that. The queryset is only used for checking updates, so I'm unclear about exactly what isn't working when making a GET request.

If I use the above field it will give me json serialization error in response.

Could you give an example of that.

I don't think there's a documentation issue, but there could be something else to unpick here (or not) you shouldn't need to use .values_list('pk', flat=True) and our test cases seem to suggest that the existing behavior works as expected. If it doesn't then that's something we need to look into further.

lovelydinosaur avatar Jul 12 '17 16:07 lovelydinosaur

If I use the above field it will give me json serialization error in response.

Couldn't reproduce any error, using If I use the above field it will give me json serialization error in response.

Happy to reconsider this issue, given a traceback and more information, but closing in its current state.

lovelydinosaur avatar Jul 12 '17 16:07 lovelydinosaur