Skip to content

DRF Integration #210

@StanGirard

Description

@StanGirard

ANSWER ON THIRD COMMENT

Hello guys,

Thanks a lot for this outstanding work.

I'm trying to integrate dj-organization into my DRF project and I'm having a bit of an issue. I'm struggling to filter the results the view sends back based on the user organization. I'm fairly new to Django and I'm having difficulties integrating it with DRF.

Here is my code.

My Organization

from django.db import models
from organizations.models import Organization

# Create your models here.

class Website(Organization):
    url = models.CharField(max_length=200)

My Model which references the org

import datetime
from django.db import models
from django.utils import timezone
from org.models import Website

class Extractor(models.Model):
   Org = models.ForeignKey(Website, related_name='extractor', on_delete=models.CASCADE)
   extractor_type = models.TextChoices('Extractor', 'HEADERS IMAGES LINKS')
   url = models.CharField(max_length=200)
   result = models.JSONField(blank=True, null=True)
   type_audit = models.CharField(blank=True, choices=extractor_type.choices, max_length=20)
   task_id = models.CharField(max_length=50, blank=True, null=True)
   status_job = models.CharField(max_length=30, blank=True, null=True)
   begin_date = models.DateTimeField(blank=True, null=True)

   def __repr__(self):
       return '<Audit {}>'.format(self.url)

And my view for the Extractor Model

Extractor view

class ExtractorViewSet(viewsets.ModelViewSet):
    permission_classes = [permissions.IsAuthenticated]
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Extractor.objects.order_by('-begin_date')
    serializer_class = ExtractorSerializer
    filter_backends = [DjangoFilterBackend,filters.OrderingFilter]
    filterset_fields = ['type_audit', 'status_job']
    ordering_fields = ['id', 'type_audit', 'begin_date']

My question is as follow:

Where do I filter the Extractor object based on the user in the request.user so that he only sees the extractor for his organization ?

I know this isn't an Issue as per say, but I'll be interested to add the answer in your documentation and an How To for others :)

Thanks a lot. Have a great day

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions