Skip to content

Long rendering of ForeignKey fields #9192

@chrisspen

Description

@chrisspen

By default, any ForeignKey field is rendered on the autogenerated "HTML Form" page as a select widget showing all records in the database.

Usually, this is fine, but for databases with large tables containing many thousands of records, this causes this page rendering to take a long time, or possibly even timeout, as the page tries to render all the tags.

Django's admin handles this problem with a feature called "raw_id_fields" which allows you to specify the ForeignKey fields which should just be rendered as a simple CharField containing the record ID, greatly speeding up rendering performance.

Does DRF have a similar feature? I can find nothing in the docs.

A similar question has been asked on Stackoverflow, and the suggested solution is to explicitly define your "raw id field" as a class attribute in your serializers.ModelSerializer, but this seems clunky and difficult to make dynamic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions