Replies: 3 comments
-
Hello @chrisspen,
|
Beta Was this translation helpful? Give feedback.
-
Is there a way to automate this, so it automatically configures this for all foreign key fields? I have some code that auto-generates DRF interfaces for lots of models, each that might have several foreign key fields, so I want to avoid manually defining a bunch of custom class attributes. Using that SO post as a reference, and delving into the DRF code, I came up with this:
I'm guessing I could adapt that to use your PrimaryKeyRelatedField, but is there a more elegant way than modifying the internal |
Beta Was this translation helpful? Give feedback.
-
so I believe |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions