Giter Club home page Giter Club logo

Comments (2)

sheppard avatar sheppard commented on June 6, 2024

Yes, this is a bug in wq.db's ModelSerializer, which converts all incoming "" to None before running the validation. A temporary workaround would be to create a custom ModelSerializer that restores the blank value:

# myapp/serializers.py
from wq.db.rest.serializers import ModelSerializer

class MyModelSerializer(ModelSerializer):
    def to_internal_value(self, data):
        result = super().to_internal_value(data)
        if result.get('fieldname', None) is None:
            result['fieldname'] = ""
        return result

# myapp/rest.py
from wq.db import rest
from .serializers import MyModelSerializer
from .models import MyModel

rest.router.register_model(
    MyModel,
    serializer=MyModelSerializer,
)

The longer term fix is a bit more complicated. The issue narrows down to the fact that wq.db's ModelSerializer uses the HTML JSON Forms parser to handle potentially nested records. This processing happens early, so that by the time the validators run, the data is treated as if it had been submitted via a JSON API. Since JSON differentiates between null and "", but HTML does not, I had to make an arbitrary decision how to handle empty strings when converting from an HTML Form submission to JSON (see this function). I chose to treat all empty strings as null, since that was the more common interpretation in my usage. The downside is that blank=True doesn't work at all.

Django REST Framework itself does not suffer from this constraint, because it has access to both the field properties and the structure of the input data at the same time (see this function). When is_html_input() is run on a dictionary that has already been processed by HTML JSON forms, it returns False, so the allow_blank handling code doesn't run.

If you aren't using nested serializers you technically don't need HTML JSON Forms at all, though I don't currently have an easy way to disable the functionality.

from wq.db.

tomaszn avatar tomaszn commented on June 6, 2024

Another workaround: I noticed that if I set value of these non-nullable text fields to " " (a space), they are stored in the database as empty strings (without conversion to NULL).

from wq.db.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.