Comments (4)
If you only show the filter values used for filtering, how would the user enter the values? I guess if you use a separate search page for the user to enter values and another page to show the results, it would make sense to show filter criteria. Never though about that.
Currently there is no easy way to do that except as you mentioned to use get_specs()
. If you end up implementing something, contribution would be always welcome.
Some thoughts on the implementation. get_specs
is a pretty expensive call since validations could be expensive. I would start with adding a simple specs
cached property to the filterset (basically move https://github.com/miki725/django-url-filter/blob/master/url_filter/filtersets/base.py#L314-L324 to a cached property) and then use that property in the filter()
call. Also since there could be many possible ways to render those specs, I think it would make sense to create some sort of renderer which would take as input the filter specs and output some string output which could be HTML or could be something else. For example one renderer could output as HTML description list.
API might looks something like this:
>>> class ShopFilterSet(FilterSet):
... name = Filter(form_field=forms.CharField(label='Name', max_length=32))
... employees = Filter(form_field=forms.IntegerField(label='Employees', min_value=0))
>>> fs = ShopFilterSet(QueryDict('name=Starbucks&employees__gt=5')
>>> DescriptionListFormatter(fs.specs).render()
<dl>
<dt>Name</dt>
<dd>Starbucks</dt>
<dt>Employees</dt>
<dd>5</dd>
</dl>
>>> DescriptionListFormatter(fs.specs, with_lookups=True).render()
<dl>
<dt>Name</dt>
<dd>= Starbucks</dt>
<dt>Employees</dt>
<dd>> 5</dd>
</dl>
from django-url-filter.
If you only show the filter values used for filtering, how would the user enter the values?
Not really relevant to me how the user enters values alas. Different unrelated issue. For example a pile of links on one page I have present filtered views of certain models. The exact filter is visible in the URL as they are GET requests (links with the filter in the URL). But it's an ugly Django filter description, and I would like pretty version.
I guess if you use a separate search page for the user to enter values and another page to show the results, it would make sense to show filter criteria. Never though about that.
Spot on! Or even more simply there are links from other pages in tables that provide drill downs into the items that are behind a given statistic.
Basically the following might be a pro-forma for what I want and may need to implement:
- Check each get parameter for validity (is it a valid Django queryset filter parameter)? If so:
- Replace all the trailing __operator strings with pretty versions. eg. "date__gt=somedate" becomes "date > somedate"
- Replace all PK references with str versions of the related object. eg. "player=20" becomes "player = Jack Sprat"
For a start anyhow. I'd need to experiment a little. Could use the get params or the get_specs() output as a source in a sense (thinking the latter deals with step 1).
there could be many possible ways to render those specs,
Yep, indeed. Some decisions to make, but a simple list of plain English versions of filter crieria is a good place to start, that can be rendered any number of ways by an end user. I imagine a list of items like the two examples above. Plain language renditions of django filter criteria (which are failry lucid already, just a tad geeky for presenting to the public.
Your suggested API looks pretty good too. Not a bad idea at all. Really there are plenty of options for plainer language presentation, though I'd choose one I guess hat leaves final rendering and styling options to the template that lists the filter specs.
from django-url-filter.
Replace all PK references with str versions of the related object. eg. "player=20" becomes "player = Jack Sprat"
That will complicate things. FilterSet
has no clue that 20
is a PK of a model. It simply validates that 20
is an integer. Knowledge what do with with 20
for filtering is all in filter backend since filterset is db agnostic.
I see 2 approaches:
- add hooks in backend for renderer to use to decode values
- made renderers db-specific. maybe something similar to
CallableFilter
from django-url-filter.
Thanks for the thoughts.
Personally I'd have thought the issue no so much db-specific and model related. That is, knowing the model that the filter applies to, I can divine that it is ForeignKey and do the translation. I already do similar in a django extension I'm building.
And when I look at my FilterSet call (quoted from my code base):
fs = FilterSet(data=self.request.GET, queryset=self.model.objects.all())
I see it has the queryset which has a model attribute which reveals the model and hence the definition of the fields. Am I misunderstanding something?
You see if I had to write such a pretty printer I could do it standalone, and/or as a method inside of FilterSet (which I suspect is easier as FilterSet already has all the parsing smarts and it's only at the point of field=value, that a decision need be made based on the definition of field, whether it's a scalar or a relationship) and offer a pull request at some stage. What's evident is it hasn't been done, so I may make time to do it eventually ... maybe ;-). I mean I want pretty renders and it's on my list and I started on Django in no small part because of it's bold and loud claim to DRY which I abide and live by as best I can ...
from django-url-filter.
Related Issues (20)
- lookups not limited according to field type
- __icontains is not working on list // arrayfields HOT 1
- Deprecated method ungettet_lazy HOT 9
- Filtering from custom DRF actions HOT 1
- Enabling all fields filtering by default
- Ignore empty fields HOT 1
- How can I filter with OR condition?
- UUID filter with lookup startswith HOT 4
- Package maintenance HOT 4
- Not compatible with Django 4.2.1 HOT 3
- ArrayField Support of Postgresql
- Fix the docs in url_filter/integrations/drf_coreapi.py HOT 2
- UTC datetimes in the database queried against the
- Support for []-style array arguments
- ISO datatime format HOT 2
- DateTime __date filter not working HOT 6
- CoreAPIURLFilterBackend causing recursion depth exceeded HOT 5
- Filtering on email field throws ValidationError HOT 1
- Error when trying to filter by a date range of a datetime field HOT 1
- How can I shorten the request
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-url-filter.