Check the modelfield's class instead

This commit is contained in:
Yuri Prezument 2014-01-12 16:30:26 +02:00
parent 0fd0454a5c
commit cd9a4194ea

View File

@ -804,6 +804,10 @@ class ModelSerializer(Serializer):
issubclass(model_field.__class__, models.PositiveSmallIntegerField):
kwargs['min_value'] = 0
if model_field.null and \
issubclass(model_field.__class__, (models.CharField, models.TextField)):
kwargs['allow_none'] = True
attribute_dict = {
models.CharField: ['max_length'],
models.CommaSeparatedIntegerField: ['max_length'],
@ -821,15 +825,10 @@ class ModelSerializer(Serializer):
kwargs.update({attribute: getattr(model_field, attribute)})
try:
field_class = self.field_mapping[model_field.__class__]
return self.field_mapping[model_field.__class__](**kwargs)
except KeyError:
return ModelField(model_field=model_field, **kwargs)
if issubclass(field_class, CharField) and model_field.null:
kwargs['allow_none'] = True
return field_class(**kwargs)
def get_validation_exclusions(self):
"""
Return a list of field names to exclude from model validation.