You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that explicitly declared fields will not have their attributes overridden by read_only_fields shortcut. #3460
@TheProdigyFilippo I'm pretty sure you'll also need to set fields = ('info', ) as the example in the docs. Also, please note that the discussion group is the best place to take this discussion and other usage questions. Thanks!
@jpadilla Thank for your interest, but you're wrong. Firstly, in the documentation there's no written anywhere that read_only_fields should work only in conjuction with fields. And it shouldn't. Example is EXAMPLE only. If there would be additionaly some crappy global variable in the docs does it mean, that read_only_fields works only when you also use this global variable?
read_only_fields is not working when you add field explicitly to the serializer class and it is a bug. Even adding this field to fields, what you proposed (but what is stupid) is not working. Real life example:
But why the necessity of adding explicitly created Field to the field tuple is stupid? Because you can use exclude which is mutually exclusive with fields and read_only_fields would be useless.
I hope that now you understand the bug. If you have any more questions, I'll do my best to answer, give few more examples and so on. Of course you can also use the discussion group.
Reacted by Paarth Shah and Vladislav Kazakov
Reacted by Juan Medina, Eugene, Johannes Postler, Winston Good, Daniele Miele, Šarūnas Nejus, Emmanuel Meric de Bellefon, seancahill-shipwell, Hakan Taşköprü, Josh Kersey and 7 more
Dear @xordoquy@jpadilla, excuse me for my aggresive tone. It was response for treating me like a ingoramus and sending away to the docs (I've read it), discussion group and superfast closing this issue by Jose. I'm very sorry again for that. I wanted to help you guys, DRS still is great and your work also is great :)
Reacted by htmall, mimimi, Paarth Shah, Hakan Taşköprü and FrankRough
The fact that we close pretty fast issues is linked with our bandwidth. Examples help but still require a lot of our time which is limited. A failing test case definitively helps understanding the issue and figure whether or not it's a DRF bug and whether or not it needs to be fixed.
if you've specified a field explicitly it doesn't make sense to have it in read_only_fields
Can't agree with you. It would be definitely better (for a coding style) to take advantage of configuration in one place. In my example I'm forced to set read_only property in two places (in field and read_only_fields) or redefine all model fields in serializer and not use read_only_fields to achieve consistency. But it would be great to set read_only property for all wished fields in read_only_fields.
I think it'd be more consistent if read_only_fields would also apply to explicitly defined fields.
After all, we do need to list them to the fields list so they are taken into account.
The major issue I'd see to this change would be the following case:
class PersonSerializer(serializers.ModelSerializer):
info = serializers.Field(source='info', read_only=False)
class Meta:
model = Person
read_only_fields = ('info',)
We had this conversation when the API for 3.0 was being discussed. The summary of it was this:
All these convenience flags would be removed, in order to promote a cleaner API.
We'd make a special exception for read_only_fields — because it was such a common use-case.
The use-case is when you have a ModelSerializer where you are not explicitly defining fields but quickly want to mark a field as read-only: the obvious example of this is a user field, where you set the user based on request.user and not on submitted data.
We decided that once users stepped onto the path of explicitly defining fields they would be responsible for moving the read-only declaration there, in line with 1 above.
Drop read_only_fields entirely, because it's such an ugly little shortcut.
Just on this. It is (an ugly little shortcut) — but it's handy for newcomers to DRF: you can get up and running with ModelSerializer, and get some great results without learning too much. Then you realise need to make your user reference read-only and, without the shortcut, there's instantly a whole load of unfamiliar API that you need to take-in. With the shortcut you can keep progressing and loop back to the Fields API, a good while later.
So.. This ain't fixed yet right? Came here through Google.. I don't agree with dropping the shortcut, it's as it was pointed out a "common scenario". If you have a bunch of fields customized say 15 it feels kinda silly to place a read_only flag on each one vs using a shortcut that was actually created for this purpose... Specific use-case: I have a django live production model with a lot of fields, you need the api to throw camelCase field names, so you have to explicitly set each field to rename it, and mark them read_only.. (this points out a feature request maybe, labels dirs on Meta)
For me either go with (1) drop the shortcut if it's not going to work as a shorcut, or (2) properly support the use-case when you have a bunch of explicitly defined fields on a ModelSerializer
It's not that it "ain't fixed", it's that it's not something I'd want to support.
If there's a field explicitly declared on the serializer, then that's what it'll use. You don't need additional shortcuts if you're explicitly declaring it.
If it's the case that you want a subclass of that serializer where you modify a field to be read only then add the field explicitly on the subclass and mark it read only. Job done.
I have requested a change on the PR. please try to address that. @samuelchen can you also check the related PR and suggest any additional edge case/test case there as well?
I have requested a change on the PR. please try to address that. @samuelchen can you also check the related PR and suggest any additional edge case/test case there as well?
changed the title [-]read_only_fields not working when specifying field explicitly[/-][+]Document that explicitly declared fields will not have their attributes overridden by `read_only_fields` shortcut.[/+]on Mar 21, 2024
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.
I've opened a PR that documents this: read_only_fields is ignored for fields declared explicitly on the serializer, so you need read_only=True on the field itself.
Activity
jpadilla commentedon Oct 1, 2015
@TheProdigyFilippo I'm pretty sure you'll also need to set
fields = ('info', )as the example in the docs. Also, please note that the discussion group is the best place to take this discussion and other usage questions. Thanks!TheProdigyFilippo commentedon Oct 1, 2015
@jpadilla Thank for your interest, but you're wrong. Firstly, in the documentation there's no written anywhere that
read_only_fieldsshould work only in conjuction withfields. And it shouldn't. Example is EXAMPLE only. If there would be additionaly some crappy global variable in the docs does it mean, thatread_only_fieldsworks only when you also use this global variable?read_only_fieldsis not working when you add field explicitly to the serializer class and it is a bug. Even adding this field tofields, what you proposed (but what is stupid) is not working. Real life example:And the result:
But why the necessity of adding explicitly created Field to the
fieldtuple is stupid? Because you can useexcludewhich is mutually exclusive withfieldsandread_only_fieldswould be useless.I hope that now you understand the bug. If you have any more questions, I'll do my best to answer, give few more examples and so on. Of course you can also use the discussion group.
xordoquy commentedon Oct 1, 2015
@TheProdigyFilippo please refrain from using aggressive tone. A few things to keep in mind:
TheProdigyFilippo commentedon Oct 1, 2015
Dear @xordoquy @jpadilla, excuse me for my aggresive tone. It was response for treating me like a ingoramus and sending away to the docs (I've read it), discussion group and superfast closing this issue by Jose. I'm very sorry again for that. I wanted to help you guys, DRS still is great and your work also is great :)
xordoquy commentedon Oct 1, 2015
The fact that we close pretty fast issues is linked with our bandwidth. Examples help but still require a lot of our time which is limited. A failing test case definitively helps understanding the issue and figure whether or not it's a DRF bug and whether or not it needs to be fixed.
lovelydinosaur commentedon Oct 1, 2015
Correct - if you've specified a field explicitly it doesn't make sense to have it in read_only_fields. We could as an enhancement error loudly?
TheProdigyFilippo commentedon Oct 1, 2015
Can't agree with you. It would be definitely better (for a coding style) to take advantage of configuration in one place. In my example I'm forced to set read_only property in two places (in field and read_only_fields) or redefine all model fields in serializer and not use read_only_fields to achieve consistency. But it would be great to set read_only property for all wished fields in read_only_fields.
xordoquy commentedon Oct 1, 2015
I think it'd be more consistent if
read_only_fieldswould also apply to explicitly defined fields.After all, we do need to list them to the
fieldslist so they are taken into account.The major issue I'd see to this change would be the following case:
carltongibson commentedon Oct 1, 2015
We had this conversation when the API for 3.0 was being discussed. The summary of it was this:
read_only_fields— because it was such a common use-case.The use-case is when you have a ModelSerializer where you are not explicitly defining fields but quickly want to mark a field as read-only: the obvious example of this is a
userfield, where you set the user based onrequest.userand not on submitted data.We decided that once users stepped onto the path of explicitly defining fields they would be responsible for moving the read-only declaration there, in line with 1 above.
lovelydinosaur commentedon Oct 1, 2015
Agree. And if the field has been defined explicitly - that's where.
read_only_fieldsentirely, because it's such an ugly little shortcut.read_only_fields.But having an explicitly defined field that does not match the definition its been given isn't going to happen.
carltongibson commentedon Oct 1, 2015
Just on this. It is (an ugly little shortcut) — but it's handy for newcomers to DRF: you can get up and running with
ModelSerializer, and get some great results without learning too much. Then you realise need to make youruserreference read-only and, without the shortcut, there's instantly a whole load of unfamiliar API that you need to take-in. With the shortcut you can keep progressing and loop back to the Fields API, a good while later.lovelydinosaur commentedon Oct 1, 2015
Indeed, so we're not taking (1), and had really already made a decision on that. From my POV that leaves (2) and (3) as valid options.
twbagustin commentedon Mar 31, 2017
So.. This ain't fixed yet right? Came here through Google.. I don't agree with dropping the shortcut, it's as it was pointed out a "common scenario". If you have a bunch of fields customized say 15 it feels kinda silly to place a read_only flag on each one vs using a shortcut that was actually created for this purpose... Specific use-case: I have a django live production model with a lot of fields, you need the api to throw camelCase field names, so you have to explicitly set each field to rename it, and mark them read_only.. (this points out a feature request maybe, labels dirs on Meta)
For me either go with (1) drop the shortcut if it's not going to work as a shorcut, or (2) properly support the use-case when you have a bunch of explicitly defined fields on a ModelSerializer
lovelydinosaur commentedon Mar 31, 2017
It's not that it "ain't fixed", it's that it's not something I'd want to support.
If there's a field explicitly declared on the serializer, then that's what it'll use. You don't need additional shortcuts if you're explicitly declaring it.
If it's the case that you want a subclass of that serializer where you modify a field to be read only then add the field explicitly on the subclass and mark it read only. Job done.
17 remaining items
Jorl17 commentedon Oct 28, 2021
I have created PR #8228 to help with this.
381e35 commentedon Aug 7, 2022
Here in 2022, issue still exists.
samuelchen commentedon Dec 20, 2022
same here, even the field is not explicitly declared.
e.g. I can
updatefields inread_only_fields, such asusername,is_superuserand etc.auvipy commentedon Dec 21, 2022
I have requested a change on the PR. please try to address that. @samuelchen can you also check the related PR and suggest any additional edge case/test case there as well?
samuelchen commentedon Dec 21, 2022
I added the case to #8228 .
[-]read_only_fields not working when specifying field explicitly[/-][+]Document that explicitly declared fields will not have their attributes overridden by `read_only_fields` shortcut.[/+]stale commentedon Oct 18, 2025
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.
abidaliamanat9 commentedon Aug 30, 2026
I've opened a PR that documents this:
read_only_fieldsis ignored for fields declared explicitly on the serializer, so you needread_only=Trueon the field itself.#10028