Support GeneratedField #9494
Unanswered
MaHryCT3
asked this question in
Potential Issue
Replies: 3 comments
-
I guess the problem goes a little earlier that build_standard_field has to recognize this field |
Beta Was this translation helpful? Give feedback.
0 replies
-
DRF plans to support new features of Django and Python new releases. so POC in welcome |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here's a snippet that seems to be doing the trick for me by overriding the # Breww has overwritten this function to handle Generated fields - see https://github.com/encode/django-rest-framework/discussions/9494
def build_standard_field(self, field_name, model_field):
"""
Create regular model fields.
"""
field_mapping = ClassLookupDict(self.serializer_field_mapping)
# If this is a GeneratedField, convert it first to its output_field as this is how it should be treated
if isinstance(model_field, GeneratedField):
model_field = model_field.output_field
field_class = field_mapping[model_field]
field_kwargs = get_field_kwargs(field_name, model_field)
# Special case to handle when a OneToOneField is also the primary key
if model_field.one_to_one and model_field.primary_key:
field_class = self.serializer_related_field
field_kwargs['queryset'] = model_field.related_model.objects
# ...rest of the function here The only part needed is the block: # If this is a GeneratedField, convert it first to its output_field as this is how it should be treated
if isinstance(model_field, GeneratedField):
model_field = model_field.output_field |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have SimpleMetadata and a get_field_info method that retrieves the field type using the label_lookup dictionary. But the new field is GeneratedField, inherited from Field so it is defined as ModelField and in the schema the field is called 'field', but actually we have GeneratedField(output_field=...) which labels the data type. Is there any support for this planned?
Beta Was this translation helpful? Give feedback.
All reactions