Code Monkey home page Code Monkey logo

Comments (18)

DusanMadar avatar DusanMadar commented on June 4, 2024 1

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.

Fettah avatar Fettah commented on June 4, 2024 1

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.

DusanMadar avatar DusanMadar commented on June 4, 2024 1

@barseghyanartur Go for it. And good comment!

from django-elasticsearch-dsl-drf.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

When did you get this error? What did you do?

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

I get this error following http://django-elasticsearch-dsl-drf.readthedocs.io/en/0.10/quick_start.html.

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

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.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

Have you declated MyModel in the Meta of MyDocument?
Have you declated MyDocument in the Meta of MySerializer?

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

Yes for both.

from django-elasticsearch-dsl-drf.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

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.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

@DusanMadar:

Did you check it with example project?

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

Not yet. I will let you know when I get to it.

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

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.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

Did you reindex after switching to 6.x? Indexes are often not compatible between versions.

from django-elasticsearch-dsl-drf.

DusanMadar avatar DusanMadar commented on June 4, 2024

Cool, reindexing helps.

from django-elasticsearch-dsl-drf.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

@DusanMadar:

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.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

@DusanMadar, @Fettah:

Thanks for research! I'll check it asap.

Update

To be fixed in version 0.12.

from django-elasticsearch-dsl-drf.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

@DusanMadar, @Fettah:

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.

barseghyanartur avatar barseghyanartur commented on June 4, 2024

Solved in 0.12.

from django-elasticsearch-dsl-drf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.