Skip to content

ModelInfoChoicesViewSet ​

Overview ​

List the choices of one field on a model's canonical serializer.

The field URL segment names a field of that serializer. A field without choices answers 404. The permissions depend on what backs the field:

  • A model field needs read on the model.
  • A model relation needs read on the model and list on the related model.
  • A serializer-only field, which is neither, needs read on the model. When it has a queryset, it also needs list on that queryset's model.

A field with static choices (a ChoiceField, or a model field with choices) returns every entry, sorted by label, with the value converted to a string. Blank entries stay in the list, since a create or update form can offer them.

A related field (PrimaryKeyRelatedField, SlugRelatedField, or a many-related field) returns the rows of the serializer field's queryset. The value is the primary key as a string, or the slug_field value for a SlugRelatedField. The label is the related model's formatted name, and options are sorted by it. The label comes from the first of these that the related model provides:

  1. A get_formatted_name() method, called on each row.
  2. The column that a string formatted_name_lookup_expression names.
  3. The formatted_name column.

A related model with none of the three raises FieldError.

get_queryset ​

Returns a queryset if the field is a relation. Return a list if the field is not a relation.

Signature ​

get_queryset(self)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes

Source ​

server/vueda/info/viewsets.py:346

has_choices ​

Report whether a serializer field offers choices, without materializing them.

RelatedField.choices and ManyRelatedField.choices are properties that read the related queryset, so a plain hasattr on the instance reads the related table. Testing the class answers the same question for every DRF field without a query. The instance test stays for a field that assigns choices in __init__ instead of declaring a property.

Signature ​

has_choices(self, field)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes
fieldyes

Source ​

server/vueda/info/viewsets.py:258

resolve_choices ​

Resolve the addressed field, the permissions it requires, and the state that get_queryset needs to build the choices.

DRF calls check_permissions before it calls the handler, so resolution cannot wait for get_queryset. Subclasses store their resolved state on the instance, raise Http404 for a field the model does not offer, and tolerate repeat calls.

Signature ​

resolve_choices(self)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes

Source ​

server/vueda/info/viewsets.py:287

validate_queryset ​

Signature ​

validate_queryset(self, serializer, fields)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes
serializeryes
fieldsyes

Source ​

server/vueda/info/viewsets.py:269

basename ​

description ​

detail ​

name ​

serializer_class ​

suffix ​

Source ​

server/vueda/info/viewsets.py:227