嗨,我有两个后台函数,它们从api获取数据并每隔几秒钟调用.save()
来更新模型
这样更新的模型是MBEvent
和MBOrders
我想使用外键在模型之间映射数据。
event_id
temp_id
runner_id
是这三个模型的主键。
我想映射
runner_1_back_odds
runner_2_back_odds
runner_1_lay_odds
runner_2_lay_odds
从MBEvent
以current_back_odds
和current_lay_odds
到MBBet
我无法获取外键以在Django中工作。我可能会用外键请求什么? 我真正想要的是,当MBEvent更新时,数据也将在MBBet中更新。外键是否可以将模型映射在一起并确保数据的完整性?
class MBEvent(models.Model):
event_id = models.BigIntegerField(primary_key=True)
event_name = models.CharField(max_length=100,null=True)
start_time = models.DateTimeField(null=True)
is_ip = models.CharField(max_length=100,null=True)
runner_1_name = models.CharField(max_length=100,null=True)
runner_2_name = models.CharField(max_length=100,null=True)
runner_1_id = models.BigIntegerField(null=True)
runner_2_id = models.BigIntegerField(null=True)
runner_1_back_odds = models.FloatField(null=True)
runner_2_back_odds = models.FloatField(null=True)
runner_1_lay_odds = models.FloatField(null=True)
runner_2_lay_odds = models.FloatField(null=True)
class MBOrders(models.Model):
temp_id = models.FloatField(primary_key=True)
event_id = models.BigIntegerField(null=True)
event_name = models.CharField(null=True,max_length=300)
runner_name = models.CharField(null=True, max_length=100)
runner_id = models.BigIntegerField(null=True)
odds = models.FloatField(null=True)
remaining = models.IntegerField(null=True)
pot_pro = models.IntegerField(null= True)
rem_pot_pro = models.IntegerField(null=True)
pot_lib = models.IntegerField(null=True)
side = models.CharField(null=True, max_length=8)
stake = models.IntegerField(null=True)
status = models.CharField(null=True, max_length=10)
time_stamp = models.DateTimeField(null=True)
class MBBet(models.Model):
runner_id = models.BigIntegerField(primary_key=True)
runner_name = models.ForigenKey(MBEvent,on_delete=MODELS.CASCADE)
event_name = models.CharField(null=True,max_length=300)
current_back_odds = models.ForigenKey(MBEvent,on_delete=MODELS.CASCADE)
current_lay_odds = models.ForigenKey(MBEvent,on_delete=MODELS.CASCADE)
bet_odds = models.FloatField(null=True)
side = models.CharField(null=True, max_length=8)
stake = models.IntegerField(null=True)
status = models.CharField(null=True, max_length=10)
time_stamp = models.DateTimeField(null=True)
答案 0 :(得分:0)
一旦在模型中设置了ForeignKey字段,就可以像这样使用它们:
mb_event_one = MBEvent()
mb_event_two = MBEvent()
mb_bet = MBBet()
mb_bet.current_back_odds = mb_event_one
mb_bet.current_lay_odds = mb_event_two
mb_bet.save()
这样,您可以直接从父级访问子级属性:
mb_bet.current_back_odds.event_id # == mb_event_one.event_id
mb_bet.current_lay_odds.event_name # == mb_event_two.event_name
我建议您为ForeignKey字段使用清晰准确的名称。如果您链接一个事件,则最好致电您的字段hosting_event
或类似的名称。
编辑::好的,我知道您要执行的操作。
ForeignKey字段用于将模型链接在一起,而不是字段。 您应该这样使用它们:
class MBRunner(models.Model):
# ...
class MBEvent(models.Model):
event_id = models.BigIntegerField(primary_key=True)
runner = models.ForeignKey(MBRunner, on_delete=models.CASCADE)
my_runner = MBRunner()
my_event = MBEvent(runner=my_runner)
这样,您可以访问事件中所有跑步者的属性。
my_event.runner.runner_name # == my_runner.runner_name
我建议您再次阅读documentation。