Skip to content

HStore: This field may not be null. #5644

Closed

Description

@dungdm93

Steps to reproduce

class Product(models.Model):
    title = CharField(max_length=512)
    attributes = HStoreField(null=True, blank=True)

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('title', 'attributes')

Some try:

p = Product(title="foobar", attributes={'first': 'value', 'second': None})
p.save()     # Save success. No error here!


# Now, Let's try with Serializer
data = {"title": "foobar", "attributes": {"first": "value", "second": None}}
q = ProductSerializer(data=data)
q.is_valid()
#> False
q.errors
#> {'attributes': ['This field may not be null.']}

Expected behavior

q is valid

Actual behavior

As you can see, q is invalid with an error {'attributes': ['This field may not be null.']}.
When I remove "second": None from attributes. q is valid

Activity

  1. rpkilby commented on Dec 4, 2017

    @rpkilby
    Contributor

    This issue seems valid. Django 1.11 added the ability to store nulls as values in addition to strings.

    https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#hstorefield

  2. carltongibson commented on Dec 4, 2017

    @carltongibson
    Collaborator

    Ideally the error message would be more specific too. (It would be nice if second were mentioned, so I knew where the problem lay, assuming the null=False case.)

  3. dungdm93 commented on Dec 4, 2017

    @dungdm93
    Author

    @rpkilby I see. But It would be nice if Rest framework also support this too.

  4. rpkilby commented on Dec 5, 2017

    @rpkilby
    Contributor

    Ideally the error message would be more specific too. (It would be nice if second were mentioned, so I knew where the problem lay, assuming the null=False case.)

    I see two possible solutions:

    • Prefix all child errors with some common message.
      {"attributes": ['Error validating value for key "second": This field may not be null.']}
    • Collect the errors in a dictionary and re-raising those errors on the field.
      {"attributes": {"second": ["This field may not be null."]}}

    I'm not a fan of the former, as it seems error-prone to try and modify ValidationErrors. eg, the detail could be a string, dict, or list of messages.

    The latter approach is simple, since we just need to store the existing error detail without altering it.

    • probably friendlier for APIs, since you have errors stored by key/index
    • probably less friendly to HTML forms, but composite fields already have issues with forms anyway.
  5. carltongibson commented on Dec 5, 2017

    @carltongibson
    Collaborator

    OK, second option looks prettier.

    probably less friendly to HTML forms, but composite fields already have issues with forms anyway.

    Exactly. We have to live with that. HTML forms are what they are. (Not a reason not to do it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions