Repository: encode/django-rest-framework

Issue:
Possibly easy to make typo: `readonly_fields` in serializers
In DRF serializers, one way to make fields read-only is to use the `read_only_fields` attribute:

```python
class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = ('id', 'account_name', 'users', 'created')
        read_only_fields = ('account_name',)
```

There is similar functionality in the Django admin, only there it is spelled [`readonly_fields`](https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields):

```python
class AccountAdmin(admin.ModelAdmin):
    fields = ('id', 'account_name', 'users', 'created')
    readonly_fields = ('account_name',)
````

If one is familiar with the Django admin, they are somewhat likely to spell it wrong in serializers (hmm I'm not talking about myself of course), with possibly bad results.

I have noticed that in several places DRF contains preemptive checks against common errors and raises helpful warnings for them. Do you think it makes sense to do something like that in case someone writes `readonly_fields` in a serializer? (I'll be happy to write a patch if so.)


Discussion:
Would probably make sense.

Resolving patch:
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -1290,6 +1290,15 @@ def get_extra_kwargs(self):
                 kwargs['read_only'] = True
                 extra_kwargs[field_name] = kwargs
 
+        else:
+            # Guard against the possible misspelling `readonly_fields` (used
+            # by the Django admin and others).
+            assert not hasattr(self.Meta, 'readonly_fields'), (
+                'Serializer `%s.%s` has field `readonly_fields`; '
+                'the correct spelling for the option is `read_only_fields`.' %
+                (self.__class__.__module__, self.__class__.__name__)
+            )
+
         return extra_kwargs
 
     def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs):


Test patch:
diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py
--- a/tests/test_model_serializer.py
+++ b/tests/test_model_serializer.py
@@ -10,6 +10,7 @@
 import decimal
 from collections import OrderedDict
 
+import pytest
 from django.core.exceptions import ImproperlyConfigured
 from django.core.validators import (
     MaxValueValidator, MinLengthValidator, MinValueValidator
@@ -1064,3 +1065,18 @@ class Meta:
 
         child_expected = {'parent': 1, 'value': 'def'}
         self.assertEqual(child_serializer.data, child_expected)
+
+
+class Issue4897TestCase(TestCase):
+    def test_should_assert_if_writing_readonly_fields(self):
+        class TestSerializer(serializers.ModelSerializer):
+            class Meta:
+                model = OneFieldModel
+                fields = ('char_field',)
+                readonly_fields = fields
+
+        obj = OneFieldModel.objects.create(char_field='abc')
+
+        with pytest.raises(AssertionError) as cm:
+            TestSerializer(obj).fields
+        cm.match(r'readonly_fields')