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

BooleanField and NullBooleanField treated as type=string with dump_config #69

Closed
tomaszn opened this issue Sep 26, 2017 · 2 comments
Closed
Milestone

Comments

@tomaszn
Copy link
Contributor

tomaszn commented Sep 26, 2017

Survey(models.Model):
    date = models.DateField()
    valid_survey = models.BooleanField()

For such a model ./manage.py dump_config outputs string as the type of valid_survey:

            "form": [
                {
                    "name": "date",
                    "bind": {
                        "required": true
                    },
                    "type": "date",
                    "label": "Date"
                },
                {
                    "name": "valid_survey",
                    "type": "string",
                    "label": "Valid Survey"
                }
            ]

Consequently, wq maketemplates renders it as a simple text input which is not a best option.
If these two (Null)BooleanField field types were treated as type "select one", they would hopefully be rendered as choice buttons by wq maketemplates.

@sheppard
Copy link
Member

Yes, there isn't an XLSForm equivalent for boolean fields, so a ChoiceField would be the most straightforward alternative. If you want to take a look at this, it could be addressed in ModelSerializer by customizing the field mapping. As a start, you could extend get_fields_for_config():

    def get_fields_for_config(self):
        fields = self.get_fields()
        for name, field in list(fields.items()):
            if name == 'id' or field.read_only:
                fields.pop(name)
+           elif isinstance(field, serializers.BooleanField):
+               fields[name] = serializers.ChoiceField(choices=...)
        return fields

This approach is nice because it lets DRF still view the field as a boolean (since get_fields() is unchanged), while generating a useful configuration for use by wq.app and the templates.

However, there is one complication, which is that the default detail templates assume that any field with choices is accompanied with a read-only *_label showing the label for the selected choice. So, you might also want to extend get_label_fields() to generate an equivalent label for boolean fields. The cleanest way to do this is probably to define a custom ReadOnlyField (see LocalDateTimeField for an example).

@sheppard
Copy link
Member

This is working now in wq.db 1.1.3.

sheppard added a commit that referenced this issue Jul 19, 2019
@sheppard sheppard added this to the 1.2 milestone Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants