Py学习  »  Django

使用models.PositiveIntegerField变量的Django循环

Ondřej Horečka • 6 年前 • 2428 次点击  

我有这个密码:

class Zapas(models.Model):
tym_domaci = models.ForeignKey(Tym, on_delete=models.CASCADE)
tym_hoste = models.ForeignKey(Tym, on_delete=models.CASCADE)
datum = models.DateTimeField('datum zapasu')
goly_domaci = models.PositiveIntegerField(default=0)
goly_hoste = models.PositiveIntegerField(default=0)

for x in range (goly_domaci):
    strelec = models.ForeignKey(Hrac, on_delete=models.CASCADE, limit_choices_to={Hrac.tym == tym_domaci})
    nahraval = models.ForeignKey(Hrac, on_delete=models.SET_NULL, blank=True, null=True, limit_choices_to={Hrac.tym == tym_domaci})

for x in range (goly_hoste):
    strelec = models.ForeignKey(Hrac, on_delete=models.CASCADE, limit_choices_to={Hrac.tym == tym_hoste})
    nahraval = models.ForeignKey(Hrac, on_delete=models.SET_NULL, blank=True, null=True, limit_choices_to={Hrac.tym == tym_hoste})

我要做的是,把所有进球的球员和协助进球的球员(如果有的话)都安排到每支球队。问题是,我不能在for循环中使用goly-domaci和goly-hoste,因为它们是正整数字段,而不是整数。有没有办法把PositiveIntegerField转换成整数?或者我能像这样使用for循环吗?我是python和Django的新手,所以我真的不知道如何解决它。谢谢你的帮助:-)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50428
文章 [ 1 ]  |  最新文章 6 年前