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

IS NULL with REST API #122

Open
coleifer opened this issue Feb 1, 2014 · 4 comments
Open

IS NULL with REST API #122

coleifer opened this issue Feb 1, 2014 · 4 comments

Comments

@coleifer
Copy link
Owner

coleifer commented Feb 1, 2014

Looks to not be working.

@rammie
Copy link

rammie commented Jul 1, 2014

+1 for a fix.

Since the api doesn't support negation, I'm guess that 'is' implies 'is null'. I needed this to work, so I hacked it in by overriding RestResource.process_query and treating the 'is' operator differently. Any thoughts around a better implementation?

    def process_query(self, query):
        ...
        # clean and normalize the request parameters
        for key in request.args:
            if '__' in key:
                expr, op = key.rsplit('__', 1)
                if op not in DJANGO_MAP:
                    expr = key
                    op = 'eq'
            else:
                expr = key
                op = 'eq'

            # OP_IS implies that the value is None
            if op == 'is':
                raw_filters[expr] = [(op, [None])]
            else:
                raw_filters.setdefault(expr, [])
                raw_filters[expr].append((op, request.args.getlist(key)))
        ...
        return query

@jmtoball
Copy link

jmtoball commented Jul 1, 2014

The API supports negation, at least in the current version. But that does not change much here.
I did this similarly, but patched "null": OP_IS into the DJANGO_MAP and then also set the value to None if the operator is OP_IS.

The setting of the value to None and thus the override of process_query would probably not be necessary if db_value of a ForeignKey would return None for empty strings. Is that an option?

@rammie
Copy link

rammie commented Jul 1, 2014

  1. Sweet, I forgot that master now supports negation.
  2. I thought about patching a different operator into the DJANGO_MAP but decided against it as the following usage would be confusing:
model.filter(field__null=False)
  1. This issue isn't exactly related to ForeignKeys as any nullable column would run into it. Also, for nullable CharFields/TextFields, distinguishing between NULL and the empty string is important.

@prawn-cake
Copy link

Does anybody plan to fix it officially? It would be great to get rid of this ridiculous limitation.
Django suggests quite neat way to handle it using field__isnull=True like syntax.
But it seems that it isn't supported by DJANGO_MAP
It implies to use is=None or eq=None syntax for ORM querying or pretty weird Model.field >> None.
So we have to add some adapters for this case here.

# flask_peewee/rest.py

if '__' in key:
    expr, op = key.rsplit('__', 1)
    if op not in DJANGO_MAP:
        expr = key
        if op == 'isnull':
            op = 'is'
            # transform value and 'negated' flag here if needed
        else:
            op = 'eq'
    ....

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

4 participants