Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HStoreField should map to DictField(child=CharField(allow_blank=True) #2659

Closed
DavidMuller opened this issue Mar 9, 2015 · 5 comments
Closed

Comments

@DavidMuller
Copy link

These 3 lines in serializers.py currently map a postgres HstoreField to a custom DRF DictField called CharMappingField:

class CharMappingField(DictField):
    child = CharField()

ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = CharMappingField

I believe, however, to mirror the default implementation of HstoreField, the child CharField() should have allow_blank=True. Consider how the current implementation behaves:

class DummyModel(models.Model):
    name = HStoreField()

class DummyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = TestModel

# empty string is a valid 'value' in the model
In [6]: TestModel.objects.create(name={'key': ''})
Out[6]: <TestModel: TestModel object>

# serializer rejects the same input
In [5]: TestModelSerializer().run_validation({'key': ''})
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-5-5d6d39e24f3e> in <module>()
----> 1 TestModelSerializer().run_validation({'key': ''})

/usr/local/etc/virtualenvs/gears/local/lib/python2.7/site-packages/rest_framework/serializers.pyc in run_validation(self, data)
    365             return data
    366 
--> 367         value = self.to_internal_value(data)
    368         try:
    369             self.run_validators(value)

/usr/local/etc/virtualenvs/gears/local/lib/python2.7/site-packages/rest_framework/serializers.pyc in to_internal_value(self, data)
    411 
    412         if errors:
--> 413             raise ValidationError(errors)
    414 
    415         return ret

ValidationError: {'name': [u'This field is required.']}
@DavidMuller DavidMuller changed the title CharMapping Field allow_blank=True CharMappingField allow_blank=True Mar 9, 2015
@tomchristie
Copy link
Member

Seems reasonable.

@debnet
Copy link

debnet commented May 13, 2015

Same thing for DateTimeField please.

@tomchristie
Copy link
Member

Same thing for DateTimeField please.

I don't understand how DateTimeField relates to this HstoreField ticket?

@debnet
Copy link

debnet commented May 13, 2015

It lacks "allow_blank=True" on DateField and DateTimeField.

@tomchristie
Copy link
Member

@debnet Not really related to this ticket.

@tomchristie tomchristie added this to the 3.1.4 Release milestone Jul 27, 2015
@tomchristie tomchristie changed the title CharMappingField allow_blank=True HStoreField should map to DictField(child=CharField(allow_blank=True) Jul 27, 2015
@tomchristie tomchristie modified the milestones: 3.1.4 Release, 3.2.0 Release Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants