我正在为我的应用程序创建模型。不幸的是,我正在使用公里/小时、千克二氧化碳/吨、热含量 (HHV) 等测量单位 - 总共有 30 个不同的单位。我不知道如何在 django 模型中正确保存它,或者在序列化器中使其显示正确的单元名称,包括 REST 响应中的“/”、“”、“(”。此外,我将通过 django-import 导入数据-导出模块,以便它应该识别 Excel 列,这些列将像实际单位名称一样命名。
例如:
class Units(models.Model):
km_h = models.FloatField(default=-1, null=True)
kg_co2ton = models.FloatField(default=-1, null=True)
我希望以以下形式提供这些数据:
class Units(models.Model):
km/h = models.FloatField(default=-1, null=True)
kg co2/ton = models.FloatField(default=-1, null=True)
如何编写模型和/或序列化器以使其工作并且看起来不错?
I'm creating model for my app. Unfortunately I'm working with measurements units like km/h, kg CO2/ton, heat content (HHV) - 30 different units at all. I don't know how to save it properly in django model or maybe in serializer to make it display proper unit name, including "/", " ", "(" in REST Responses. Also I will be importing data through django-import-export module so it should recognize excel columns which will be named like actual unit name.
For example:
class Units(models.Model):
km_h = models.FloatField(default=-1, null=True)
kg_co2ton = models.FloatField(default=-1, null=True)
and I would like to have this data available in the following form:
class Units(models.Model):
km/h = models.FloatField(default=-1, null=True)
kg co2/ton = models.FloatField(default=-1, null=True)
How to write model and/or serializer to make it work and look good?
发布评论
评论(1)
对于 django-import-export,您可以使用 Field 类的 ="nofollow noreferrer">
column_name
来声明列名称以匹配您的 Excel 导入电子表格:For
django-import-export
, you can use thecolumn_name
of theField
class to declare the column name to match your Excel import spreadsheet: