other
在app
下创建fixtures
目录,在里面创建mydata.json
文件或者mydata.YAML
格式文件(文件名没有强制规定),里面填写你要添加的数据,大致如下
[
{
"model": "card.WeekType",
"pk": "1",
"fields": {
"week_day": "monday",
"zh_hans": "周一"
}
},
{
"model": "card.WeekType",
"pk": "2",
"fields": {
"week_day": "tuesday",
"zh_hans": "周二"
}
}
]
调用: python3 manage.py loaddata mydata
他会搜寻每个app下的
fixtures
目录中是否存在mydata
的文件,存在即会加载里面的数据。
查看序列化器的信息
from apps.card.serializers.card_serializer import DiscountRuleSerializer
>>> a = DiscountRuleSerializer()
>>> print(repr(a))
DiscountRuleSerializer():
id = IntegerField(label='ID', read_only=True)
st_time = TimeField(format='%H:%M:%S')
end_time = TimeField(format='%H:%M:%S')
create_time = DateTimeField(label='创建时间', read_only=True)
update_time = DateTimeField(label='更新时间', read_only=True)
week_day = ChoiceField(choices=(('monday', '周一'), ('tuesday', '周二'), ('wednesday', '周三'), ('thursday', '周四'), ('friday', '周五'), ('saturday', '周六'), ('sundayabel='星期n', required=True)
discount_factor = FloatField(label='优惠折扣系数', max_value=1.0, min_value=0.0, required=True)
class Meta:
validators = [<UniqueTogetherValidator(queryset=DiscountRule.objects.all(), fields=('week_day', 'discount_factor', 'st_time', 'end_time'))>]