Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF show ForeignKey field choices

What's the best way to go about letting a frontend user know about ForeignKey field choices using Django Rest Framework? In the Browsable API these fields have a dropdown widget with all the existing objects as choices.

A custom metadata class could return the available choices for each field but the request could be very slow if there are millions of objects.

Suppose you have a model similar to below and there's only 5 unit objects. How would you go about listing the unit choices?

class OrderLine(models.Model):
    order = models.ForeignKey(Order)
    product = models.ForeignKey(Product)
    unit = models.ForeignKey(Unit)
like image 784
bdoubleu Avatar asked Sep 03 '25 06:09

bdoubleu


1 Answers

I ended up implementing a custom metadata class that adds foreign key choices to an OPTIONS request based on the serializer attribute extra_choice_fields. This way you can choose which fields to provided choices for on each serializer and which to not include (ex. exclude fields with a lot of objects).

from rest_framework.metadata import SimpleMetadata
from rest_framework.relations import ManyRelatedField, RelatedField

from django.utils.encoding import force_text


class ChoicesMetadata(SimpleMetadata):

    def get_field_info(self, field):
        field_info = super().get_field_info(field)
        if (isinstance(field, (RelatedField, ManyRelatedField)) and
                field.field_name in getattr(field.parent.Meta, 'extra_choice_fields', [])):
            field_info['choices'] = [{
                'value': choice_value,
                'display_name': force_text(choice_name, strings_only=True)
            } for choice_value, choice_name in field.get_choices().items()]
        return field_info
like image 138
bdoubleu Avatar answered Sep 04 '25 23:09

bdoubleu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!