简体   繁体   English

在这种情况下如何制作 django model?

[英]How can I make django model in this situation?

I make a stock market project using django.我使用 django 做了一个股票市场项目。 So I want to make 2 database table.所以我想制作2个数据库表。

  1. table stocklist (ticker, name)表库存清单(股票代码,名称)
  2. table stockPrice (code, date, price, open, high, close etc...)表 stockPrice (代码、日期、价格、开盘价、最高价、收盘价等...)

I want to make second table name is stock ticker.我想让第二个表名是股票行情。 so I though this is many to one relation.所以我虽然这是多对一的关系。 And I made django model.py我做了 django model.py

class StockList(models.Model):
    ticker= models.CharField(max_length=10,primary_key=True)
    company = models.CharField(max_length=50)
    last_update = models.DateTimeField()

class stockPrice(models.Model):
    ticker = models.ForeignKey('stockList', on_delete=models.CASCADE)
    date = models.DateTimeField()
    open = models.FloatField()      
    high =models.FloatField()      
    low = models.FloatField()       
    close = models.FloatField()     
    diff = models.FloatField()  
    volume = models.FloatField()

But it isn't work.但这行不通。 How I make django model?我如何制作 django model? And is it many to one relation?它是多对一的关系吗? Thank you.谢谢你。


I want to make DB like this.我想做这样的数据库。 So Am I make all class in models.py?那么我是否在models.py中制作了所有class? enter image description here在此处输入图像描述

In class stockPrice , try to replace:在 class stockPrice ,尝试替换:

ticker = models.ForeignKey('stockList', on_delete=models.CASCADE)

with:和:

ticker = models.ForeignKey(StockList, to_field='ticker', on_delete=models.CASCADE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django - model 在字符串 'MM/DD/YYYY' 中有日期,在这种情况下我如何 order_by(date)? - Django - model have date in string 'MM/DD/YYYY' how can I order_by(date) in this situation? Django:在这种情况下,我该如何迁移? 迁移不起作用 - Django:In this situation, how can I migrate? migrate is not working 如何在Django模型上创建一个crontab字段 - How can I make a crontab field on Django model 如何制作 Django model 表单,其字段名称与 model 字段名称不同? - how can I make a Django model form with a field name in the form different from the model field name? 如何使 django model 将其文件上传到以 model 实例命名的文件夹? - How can I make a django model upload its files to a folder named after the model instance? 如何制作模型,Django - How to make model, django 在这种情况下如何循环发电机? - How can I loop a generator in this situation? 在Django中创建另一个实例后,如何更改从模型类实例派生的变量? - How can I make a variable derived from a model class instance change as soon as another instance is created in Django? 如何使午夜和时区的Django模型日期时间字段数据可识别? - How can I make django model datetime field data of midnight and timezone aware? 我如何制作一个页面,该页面将始终显示Django模型中的最新帖子 - How can I make a page that will always display the latest post from my Django Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM