Keystone-classic: Relationship filter not equal?

Created on 20 Apr 2018  路  5Comments  路  Source: keystonejs/keystone-classic


I was wundering if it's possible to filter a relationship field value that does NOT equal a certain string?

Expected behavior



With following model you should only be able to select pages in the page relationship field (on pages with the template set to 'home') that do not have the template set to 'home':

const Page = new keystone.List('Page');

Page.add({
    template: { type: Types.Select, options: getTemplates(), emptyOption: false, default: ['default'] },
    page: { type: Types.Relationship, label: 'Page', ref: 'Page', required: false, filters: { state: 'published', template: '!home' }, dependsOn: { template: ['home'] } },
});

Page.register();

Actual/Current behavior



I can't select any pages in the relationship field. Obviously this doesn't work: '!home'

Environment

| Software | Version
| ---------------- | -------
| Keystone | 4.0.0-beta.8
| Node | 9.3.0

question

All 5 comments

I guess you want to use the $ne operator in your filter:

filters: { state: 'published', template: { $ne: 'home' } }

MongoDB operators can be used in many places thorough KeystoneJS!

Cool thanks!
The $nin operator is even better in my case: https://docs.mongodb.com/manual/reference/operator/query/nin/

Not working for me - Tried 4.0.0-beta.5, 4.0.0-beta.8 and 4.0.0

@stennie I don't know if I should tag you here, but I think this is something that the /api/list/get is not supported correctly/or not supporting the filter by object feature (I think).

Adding filter that is an object, not of type string ends up with a query with query param something like:
filters: { title: {$eq: 'Home'} }

result: filters[yourfieldname][value]=encodeURIComponent(value)

since value in this case is an object, it will return "%5Bobject%20Object%5D", which will fails the /api/list query.

If in turn I try to make filters a string, like

filters: { title: '{$eq: \'Home\'}' }

to bypass the issue with encodeURIComponent, the api call will pass to

req.list.addFiltersToQuery(filters)

which in turn does not parse the string back to a JSON object, and ends up with a filter as { title: /{\$eq:'Home'}/i } which is a regex.

I'm using "keystone": "4.0.0-beta.5"

It would be good if we can support this feature, as it will help narrow down the result for filtering relations.

@danlehoang I have exactly the same problem, did you solve this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stennie picture stennie  路  5Comments

sorryididntmeantto picture sorryididntmeantto  路  3Comments

javierpelozo picture javierpelozo  路  5Comments

mjmaix picture mjmaix  路  6Comments

christroutner picture christroutner  路  4Comments