datamodel-code-generator
datamodel-code-generator copied to clipboard
Add documentation for custom template
There is no document of the custom template. We should write a document on how to use the feature.
Related issues
https://github.com/koxudaxi/datamodel-code-generator/issues/144
I was wanting to add https://github.com/snowplow/snowplow-python-tracker/blob/master/snowplow_tracker/self_describing_json.py to the template somehow, but did not really find any way to make that happen. so it would be nice to have some documentation around extending things
@camerondavison Thank you for your comment.
I was wanting to add https://github.com/snowplow/snowplow-python-tracker/blob/master/snowplow_tracker/self_describing_json.py to the template somehow,
Sorry, What did you want? Did you want to inherit the class?
I think maybe I want to wrap it in that class. Since I need to write the schema field and the model is the data.
@camerondavison I don't know how to use snowplow-python-tracker. Do you want this output?
from pydantic import BaseModel
class Model(BaseModel):
# inject schema path when generate this model
_schema_path: str = "iglu:com.acme/viewed_product/jsonschema/2-0-2"
product_id: int = "ASO01043"
price: float = 49.95
walrus: int = 1000
def to_json(self):
return {
"schema": self._schema_path,
"data": self.dict()
}
def to_string(self):
import json
return json.dumps(self.to_json())
print(Model().to_string())
# {"schema": "iglu:com.acme/viewed_product/jsonschema/2-0-2", "data": {"product_id": "ASO01043", "price": 49.95, "walrus": 1000}}
Otherwise, Would you please write an example of the output that you expect here?
Yes that is what I want.
@camerondavison Unfortunately, This code-generator can't output the schema path. But, This viewpoint looks good. I will implement it after we resolve other issues.
I added https://koxudaxi.github.io/datamodel-code-generator/custom_template/