Comments (18)
Right, after double checking everything with docs and the example app for like a million times, the whole 'Search' object has no attribute 'model'
boils down to REST_FRAMEWORK
settings.
The reason is using DjangoModelPermissionsOrAnonReadOnly
in DEFAULT_PERMISSION_CLASSES
.
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
],
So the actual solution is to set permission_classes
on MyDocumentSearchViewSet
from the snippet above, not override get_queryset()
.
from django-elasticsearch-dsl-drf.
that looks like a permission issue:
I hope adding this to your view will do the trick.
from rest_framework import permissions
from rest_framework.decorators import api_view, permission_classes
@permission_classes((permissions.AllowAny,))
class MyAnnoyingStupidView(DocumentViewSet):
pass # maybe some weird code here as well :D
from django-elasticsearch-dsl-drf.
@barseghyanartur Go for it. And good comment!
from django-elasticsearch-dsl-drf.
When did you get this error? What did you do?
from django-elasticsearch-dsl-drf.
I get this error following http://django-elasticsearch-dsl-drf.readthedocs.io/en/0.10/quick_start.html.
from django-elasticsearch-dsl-drf.
Overriding get_queryset()
does the trick:
from django_elasticsearch_dsl_drf.filter_backends import SearchFilterBackend
from django_elasticsearch_dsl_drf.views import BaseDocumentViewSet
from my_app.documents import MyDocument
from my_app.serializers import MyDocumentSearchSerializer
class MyDocumentSearchViewSet(BaseDocumentViewSet):
document = MyDocument
serializer_class = MyDocumentSearchSerializer
filter_backends = [
SearchFilterBackend,
]
search_fields = (
'name',
)
def get_queryset(self):
qs = super().get_queryset()
qs.model = self.document._doc_type.model
return qs
from django-elasticsearch-dsl-drf.
Have you declated MyModel
in the Meta of MyDocument
?
Have you declated MyDocument
in the Meta of MySerializer
?
from django-elasticsearch-dsl-drf.
Yes for both.
from django-elasticsearch-dsl-drf.
It should not be necessary to override get_queryset
.
Could you run the example project to see if it works for you?
from django-elasticsearch-dsl-drf.
Did you check it with example project?
from django-elasticsearch-dsl-drf.
Not yet. I will let you know when I get to it.
from django-elasticsearch-dsl-drf.
OK, the example project works. It uses elasticsearch-dsl==5.3.0
. With the 'default' dependencies I am able to load http://localhost:8001/search/authors/ and see data. Though, when I install elasticsearch-dsl==6.1.0
http://localhost:8001/search/authors/ returns
{
count: 0,
next: null,
previous: null,
facets: { },
results: [ ]
}
from django-elasticsearch-dsl-drf.
Did you reindex after switching to 6.x? Indexes are often not compatible between versions.
from django-elasticsearch-dsl-drf.
Cool, reindexing helps.
from django-elasticsearch-dsl-drf.
Fine. Could you see what's the difference between the example project and the documentation (which you based your code on)? It would be greatf if you could point to some weak spots that need updating.
from django-elasticsearch-dsl-drf.
Thanks for research! I'll check it asap.
Update
To be fixed in version 0.12.
from django-elasticsearch-dsl-drf.
The get_queryset
method of the BaseDocumentViewSet
has been modified in the following way.
def get_queryset(self):
"""Get queryset."""
queryset = self.search.query()
# Model- and object-permissions of the Django REST framework (
# at the moment of writing they are ``DjangoModelPermissions``,
# ``DjangoModelPermissionsOrAnonReadOnly`` and
# ``DjangoObjectPermissions``) require ``model`` attribute to be
# present in the queryset. Unfortunately we don't have that here.
# The following approach seems to fix that (pretty well), since
# model and object permissions would work out of the box (for the
# correspondent Django model/object). Alternative ways to solve this
# issue are: (a) set the ``_ignore_model_permissions`` to True on the
# ``BaseDocumentViewSet`` or (b) provide alternative permission classes
# that are almost identical to the above mentioned classes with
# the only difference that they know how to extract the model from the
# given queryset. If you think that chosen solution is incorrect,
# please make an issue or submit a pull request explaining the
# disadvantages (and ideally - propose a better solution). Couple of
# pros for current solution: (1) works out of the box, (2) does not
# require modifications of current permissions (which would mean we
# would have to keep up with permission changes of the DRF).
queryset.model = self.document._doc_type.model
return queryset
It seems to fix the issue for me. Any remarks/objections/concerns are welcome.
from django-elasticsearch-dsl-drf.
Solved in 0.12.
from django-elasticsearch-dsl-drf.
Related Issues (20)
- Make it possible to specify search on sub-field (not only on main field)
- SuggesterFilterBackend not returning expected SerializerMethod responses. HOT 5
- Support default_lookup NestedFilteringFilterBackend HOT 1
- LimitOffsetPagination count behavior HOT 2
- [Question] Explanation of books plural from book?
- [Question] How to run example ?
- Standard analyzer is not applied by default HOT 1
- How does nested field work?
- how to search any query in multiple or all indexes or multiple documents? HOT 1
- Geolocation 'search_phase_execution_exception' error 'failed to find geo field [location]') HOT 9
- distutils Version classes are deprecated HOT 1
- How to use ignore_above in below case ? HOT 2
- why DocumentViewSet does not working? HOT 4
- How to filter array for item1 and item2
- FacetedFilterSearchFilterBackend not working when filtering using "__in" operator HOT 1
- Support for regex in completion suggesters HOT 1
- How to do query elastic search on multiple index HOT 1
- highlights is getting dropped in DocumentSerializer HOT 3
- Overriding FilteringFilterBackend.filter_queryset
- cannot import name 'AggsProxy' from elasticsearch_dsl.search HOT 9
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-elasticsearch-dsl-drf.