mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 11:04:02 +03:00
Make autogenerated read only fields to be able to be nullable (#8536)
This commit is contained in:
parent
9f07d9edeb
commit
101aff6c43
|
@ -95,6 +95,9 @@ def get_field_kwargs(field_name, model_field):
|
||||||
(hasattr(models, 'JSONField') and isinstance(model_field, models.JSONField)):
|
(hasattr(models, 'JSONField') and isinstance(model_field, models.JSONField)):
|
||||||
kwargs['style'] = {'base_template': 'textarea.html'}
|
kwargs['style'] = {'base_template': 'textarea.html'}
|
||||||
|
|
||||||
|
if model_field.null:
|
||||||
|
kwargs['allow_null'] = True
|
||||||
|
|
||||||
if isinstance(model_field, models.AutoField) or not model_field.editable:
|
if isinstance(model_field, models.AutoField) or not model_field.editable:
|
||||||
# If this field is read-only, then return early.
|
# If this field is read-only, then return early.
|
||||||
# Further keyword arguments are not valid.
|
# Further keyword arguments are not valid.
|
||||||
|
@ -104,9 +107,6 @@ def get_field_kwargs(field_name, model_field):
|
||||||
if model_field.has_default() or model_field.blank or model_field.null:
|
if model_field.has_default() or model_field.blank or model_field.null:
|
||||||
kwargs['required'] = False
|
kwargs['required'] = False
|
||||||
|
|
||||||
if model_field.null:
|
|
||||||
kwargs['allow_null'] = True
|
|
||||||
|
|
||||||
if model_field.blank and (isinstance(model_field, (models.CharField, models.TextField))):
|
if model_field.blank and (isinstance(model_field, (models.CharField, models.TextField))):
|
||||||
kwargs['allow_blank'] = True
|
kwargs['allow_blank'] = True
|
||||||
|
|
||||||
|
@ -263,6 +263,8 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
if not model_field.editable:
|
if not model_field.editable:
|
||||||
kwargs['read_only'] = True
|
kwargs['read_only'] = True
|
||||||
kwargs.pop('queryset', None)
|
kwargs.pop('queryset', None)
|
||||||
|
if model_field.null:
|
||||||
|
kwargs['allow_null'] = True
|
||||||
if kwargs.get('read_only', False):
|
if kwargs.get('read_only', False):
|
||||||
# If this field is read-only, then return early.
|
# If this field is read-only, then return early.
|
||||||
# No further keyword arguments are valid.
|
# No further keyword arguments are valid.
|
||||||
|
@ -270,8 +272,6 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
|
|
||||||
if model_field.has_default() or model_field.blank or model_field.null:
|
if model_field.has_default() or model_field.blank or model_field.null:
|
||||||
kwargs['required'] = False
|
kwargs['required'] = False
|
||||||
if model_field.null:
|
|
||||||
kwargs['allow_null'] = True
|
|
||||||
if model_field.validators:
|
if model_field.validators:
|
||||||
kwargs['validators'] = model_field.validators
|
kwargs['validators'] = model_field.validators
|
||||||
if getattr(model_field, 'unique', False):
|
if getattr(model_field, 'unique', False):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user