Skip to content

Conversation

@igorbenav
Copy link
Collaborator

@igorbenav igorbenav commented Jun 16, 2025

Using select_schema to Exclude Fields from Read Operations

Some database field types can cause issues in admin panels. The most common example is PostgreSQL's TSVector type used for full-text search, which can trigger NotImplementedError when trying to display records.

The select_schema parameter allows you to exclude problematic fields from all read operations while keeping them available for create/update operations.

Use select_schema when you encounter:

  • TSVector fields causing NotImplementedError in admin views
  • Large binary fields that slow down list views
  • Computed fields that don't need to be displayed
  • Sensitive fields that should be hidden from admin users
  • Complex JSON fields that break admin display formatting

Basic Example: Excluding TSVector Fields

from sqlalchemy import Column, Integer, String, Text
from sqlalchemy_utils import TSVectorType
from pydantic import BaseModel
from typing import Optional
from datetime import datetime

# SQLAlchemy model with TSVector for full-text search
class Document(Base):
    __tablename__ = "documents"

    id = Column(Integer, primary_key=True)
    title = Column(String(200), nullable=False)
    content = Column(Text, nullable=False)
    created_at = Column(DateTime, default=func.now())

    # This field causes NotImplementedError in admin views
    search_vector = Column(TSVectorType('title', 'content'))

# Schemas for create/update (no search_vector)
class DocumentCreate(BaseModel):
    title: str = Field(..., min_length=1, max_length=200)
    content: str = Field(..., min_length=1)

class DocumentUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    content: Optional[str] = None

# Schema for read operations (excludes problematic field)
class DocumentSelect(BaseModel):
    id: int
    title: str
    content: str
    created_at: datetime
    # search_vector field intentionally excluded!

# Register with admin
admin.add_view(
    model=Document,
    create_schema=DocumentCreate,
    update_schema=DocumentUpdate,
    select_schema=DocumentSelect,  # ✅ TSVector excluded from reads
    allowed_actions={"view", "create", "update", "delete"}
)

Closes #21

@igorbenav igorbenav added the enhancement New feature or request label Jun 16, 2025
@igorbenav igorbenav merged commit 1dc43af into main Jun 16, 2025
12 checks passed
@igorbenav igorbenav deleted the read-schema branch June 16, 2025 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using TSVector in database model raises NotImplementedError

2 participants