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

Question about integration #46

Closed
hlawatschek opened this issue Oct 10, 2023 · 1 comment
Closed

Question about integration #46

hlawatschek opened this issue Oct 10, 2023 · 1 comment

Comments

@hlawatschek
Copy link

Hi!
Thanks for your great work!
We're currently working on adding an odata API to our Django based application. I.e. we want to access data using Excel.
During our research we found this project.
We currently understand, that your project can be used to translate odata search queries for the Django ORM.
Now we have the following question:
How do you return the results to the client? Are you using the Django REST framework for the communication?
Do you by chance have an example for this that you could share?
Thanks!

  • Mark and Freddy
@OliverHofkens
Copy link
Member

OliverHofkens commented Oct 13, 2023

Hey, thanks for the nice words!

I'm personally no longer using Django, but we did use it in a Django REST project before.
These are some examples from the Django REST website that I changed to include odata-query.
I haven't tested them, but they should give you a good starting point:

from odata_query.django import apply_odata_query
from odata_query.exceptions import ODataException
from rest_framework import viewsets
from rest_framework.response import Response

from your_app import BlogPost, BlogPostSerializer

class BlogPostViewSet(viewsets.ViewSet):
    """
    A simple ViewSet for listing or retrieving blog posts.
    """
    def list(self, request):
        queryset = BlogPost.objects.all()
        
        # Apply OData filters, if passed in a query param named `filter`:
        if filter := request.query_params.get("filter"):
            try:
                queryset = apply_odata_query(queryset, filter)
           except ODataException as e:
                # An invalid query was passed, handle howevere you like.
                raise
        
        serializer = BlogPostSerializer(queryset, many=True)
        return Response(serializer.data)

It should work in a very similar fashion when using other Django REST features. E.g. when using ModelViewSet, it would probably look something like this:

class BlogPostViewSet(viewsets.ModelViewSet):
    serializer_class = BlogPostSerializer

    def get_queryset(self):
        queryset = BlogPost.objects.all()

        # Apply OData filters, if passed in a query param named `filter`:
        if filter := self.request.query_params.get("filter"):
            try:
                queryset = apply_odata_query(queryset, filter)
           except ODataException as e:
                # An invalid query was passed, handle howevere you like.
                raise

        return queryset

If you want to make it more reusable, Django REST also has the FilterBackend feature. That could look something like this:

class ODataFilterBackend(BaseFilterBackend):
        def filter_queryset(self, request, queryset, view):
             # Apply OData filters, if passed in a query param named `filter`:
            if filter := self.request.query_params.get("filter"):
                try:
                    queryset = apply_odata_query(queryset, filter)
               except ODataException as e:
                    # An invalid query was passed, handle howevere you like.
                    raise

            return queryset

class BlogPostViewSet(viewsets.ModelViewSet):
    serializer_class = BlogPostSerializer
    filter_backends = [ODataFilterBackend]

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

No branches or pull requests

2 participants