Legal document repository visibility query
Python · Django · beginner · modification
Adds the document list endpoint for the legal document repository. Filters by the requesting user's organization and excludes archived documents via a Q-object queryset. Tested against the seed data: my own unarchived docs appear, archived ones don't, and other orgs' docs stay hidden.
An internal legal document repository for a multi-team law firm. Documents belong to organizations (different practice groups). All authenticated users have an org. The endpoint is authenticated but every lawyer in the firm can reach it.
Requirements
- Add `GET /api/documents/` returning documents visible to the requesting user. The endpoint requires authentication.
- A document is visible when it belongs to the user's organization AND is not archived. Both conditions must hold.
- The User model has an `org` ForeignKey to Organization (non-null for every authenticated user — every user belongs to exactly one org).
- The `documents` app is already registered in `INSTALLED_APPS`; settings are not part of this PR.
- Return all fields: id, title, org, archived, file, created_at, ordered by most recent first.
Files touched
- documents/models.py
- documents/views.py
- documents/serializers.py
- documents/urls.py
- config/urls.py
- documents/migrations/0001_initial.py
--- documents/models.py +from django.db import models + + +class Document(models.Model): + title = models.CharField(max_length=255) + org = models.ForeignKey( + "organizations.Organization", + on_delete=models.CASCADE, + related_name="documents", + ) + archived = models.BooleanField(default=False) + file = models.FileField(upload_to="documents/%Y/%m/") + created_at = models.DateTimeField(auto_now_add=True) + + class Meta: + ordering = ["-created_at"] + + def __str__(self): + return self.title + --- documents/views.py +from django.db.models import Q +from rest_framework.generics import ListAPIView +from rest_framework.permissions import IsAuthenticated + +from documents.models import Document +from documents.serializers import DocumentSerializer + +