How to concatenate multiple fields into one in a Response Schema ? #1492
-
Hi, Considering the folowing model : class Epoch(models.Model):
...
band_code = models.CharField(max_length=1)
instrument_code = models.CharField(max_length=1)
orientation_code = models.CharField(max_length=1)
... I would like to generate a schema like this one class EpochSchema(Schema):
code: str with I tried to use a resolve_code() method that concatenates all those codes to make one. But the only way it is working is to integrate band_code, instrument_code, etc.. into my Is there a possibility to build my |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Finally found a solution here : https://docs.pydantic.dev/latest/concepts/fields/#exclude So in my example, the solution is : class EpochSchema(Schema):
code: str
band_code: str = Field(exclude=True)
instrument_code: str = Field(exclude=True)
orientation_code: str = Field(exclude=True)
@staticmethod
def resolve_code(obj):
return f"{obj.band_code}{obj.instrument_code}{obj.orientation_code}" Sorry for the unnecessary noise |
Beta Was this translation helpful? Give feedback.
Finally found a solution here : https://docs.pydantic.dev/latest/concepts/fields/#exclude
The idea is to exclude those fields when "exporting" the schema
So in my example, the solution is :
Sorry for the unnecessary noise