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

UUID filter with lookup startswith #110

Open
rodrigondec opened this issue Nov 19, 2021 · 4 comments
Open

UUID filter with lookup startswith #110

rodrigondec opened this issue Nov 19, 2021 · 4 comments

Comments

@rodrigondec
Copy link

Resume

My application uses UUIDField as a primary key. So every model inherit

class BaseModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

I want to filter by this id!

ID filter

It's possible do filter by ID.

So we can do a request like

/some_model_resource/?id={COMPLETE-UUID-HERE}

And it works 🎉 !

ID startswith filter

statswith is a supported lookup on url-filters!

supported_lookups = {
"contains",
"date",
"day",
"endswith",
"exact",
"gt",
"gte",
"hour",
"icontains",
"iendswith",
"iexact",
"in",
"iregex",
"isnull",
"istartswith",
"lt",
"lte",
"minute",
"month",
"range",
"regex",
"second",
"startswith",
"week_day",
"year",
}

So it should be possible do a request like

/some_model_resource/?id__startswith={INCOMPLETE-UUID-HERE}

It doens't work 👎

The problem

The filter calls the django form clean!

def clean_value(self, value, lookup):
"""
Clean the filter value as appropriate for the given lookup.
Parameters
----------
value : str
Filter value as given in the querystring to be validated
and cleaned by using appropriate Django form field
lookup : str
Name of the lookup
See Also
--------
get_form_field
"""
form_field = self.get_form_field(lookup)
return form_field.clean(value)

REMEMBER, I'm passing a incomplete UUID because the goal is to search with the first part of the UUID with the lookup startswith!

This causes the django.forms.fields.UUIDField.clean method to raise a ValidationError!

https://github.com/django/django/blob/a7e7043c8746933dafce652507d3b821801cdc7d/django/forms/fields.py#L1211

@rodrigondec
Copy link
Author

I'm willing to open a PR

I just need to know with which approach this should be resolved with.

Should it be added try/except on the form cleanup?

Should be created some engineering to allow 'partial values` with specific lookups (startswith, istartswith, endswith, iendswith, icontains, contains)?

Should this be treated only for UUID Fields?

@rodrigondec
Copy link
Author

@miki725 friendly ping

@felps-dev
Copy link

I think the maintener has died, just look at the pull requests session...

@rodrigondec
Copy link
Author

I think the maintener has died, just look at the pull requests session...

Sad life 😢

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

Successfully merging a pull request may close this issue.

2 participants