mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
auto_now and auto_now_add fields should be read only by default
This commit is contained in:
parent
919c5e1e01
commit
73c4e5c460
|
@ -452,6 +452,9 @@ class ModelSerializer(Serializer):
|
|||
if model_field.null or model_field.blank:
|
||||
kwargs['required'] = False
|
||||
|
||||
if not model_field.editable:
|
||||
kwargs['read_only'] = True
|
||||
|
||||
if model_field.has_default():
|
||||
kwargs['required'] = False
|
||||
kwargs['default'] = model_field.get_default()
|
||||
|
|
26
rest_framework/tests/fields.py
Normal file
26
rest_framework/tests/fields.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
"""
|
||||
General tests for relational fields.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class TimestampedModel(models.Model):
|
||||
added = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
class TimestampedModelSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TimestampedModel
|
||||
|
||||
|
||||
class ReadOnlyFieldTests(TestCase):
|
||||
def test_auto_now_fields_read_only(self):
|
||||
"""
|
||||
auto_now and auto_now_add fields should be readonly by default.
|
||||
"""
|
||||
serializer = TimestampedModelSerializer()
|
||||
self.assertEquals(serializer.fields['added'].read_only, True)
|
Loading…
Reference in New Issue
Block a user