Django_使用汇总(1)
使用django(4.1.5) 搭建股票信息后台,显示股票信息;
Stock -> models.py
class Stock(models.Model): symbol = models.CharField(max_length=45) # 股票名称 st_name = models.CharField(max_length=45) # 股票代码 st_code = models.CharField(max_length=45) # 开盘日期 st_date = models.CharField(max_length=45,primary_key=True) # 开盘 open_pce = models.DecimalField(max_digits=20, decimal_places=2) # 收盘 close = models.DecimalField(max_digits=20, decimal_places=2) # 最高价 high = models.DecimalField(max_digits=20, decimal_places=2) # 最低价 low = models.DecimalField(max_digits=20, decimal_places=2) # 涨幅 zhangfu = models.DecimalField(max_digits=20, decimal_places=2) # 行业 industry = models.CharField(max_length=45) # 备注 comments = models.CharField(max_length=45)
Stock -> testdb.py 数据插入数据库
def testdb(request): stock_info = Stock(symbol='SZ000001',st_name='平安银行',st_code='000001',st_date='2023-03-29', open_pce=45.22,close=46.25,high=46.51,low=44.95,zhangfu=2.25, industry='银行',comments=' ') stock_info.save() return HttpResponse("<p>数据添加成功!</p>")
# 创建基金类 class Fund(models.Model): fundname = models.CharField(max_length=100) fundcode = models.CharField(max_length=100) # 基金最近3个月的涨幅 fundzf_3mon = models.DecimalField(max_digits=20, decimal_places=2) fundzf_6mon = models.DecimalField(max_digits=20, decimal_places=2) fundzf_1year = models.DecimalField(max_digits=20, decimal_places=2) # 解决Django 后台添加的数据显示为Fund object 的问题 def __str__(self): return str(self.fundname), str(self.fundcode)