在django中用模板在表格中显示数值。

问题描述 投票:0回答:1

我想做一个网站来管理面包的生产,但我面临2个问题。

我有一个面包模型(Pain),允许我创建一个面包,并给这个面包一个起始价格。(nom_pain = name , prix_HT = price)

class Pain(models.Model):
nom_pain = models.CharField(max_length=25,primary_key=True)
prix_HT = models.DecimalField(max_digits=4,decimal_places=2)
pain_decouverte = models.BooleanField(null=False)
def __str__(self):
    return '{}'.format(self.nom_pain)

我有一个代表不同客户组的模型组(Groupe),(nom_groupe = name)

class Groupe(models.Model):
nom_groupe = models.CharField(max_length=30)

def __str__(self):
    return '{}'.format(self.nom_groupe)

我希望在一个页面上将每个面包的价格与一组客户的价格进行匹配,然后以表格图表的形式显示出来(在空白单元格中插入面包价格)。

            | Bread 1 | Bread 2 | Bread 3 |
    Group 1 |  0.5    |  0.6    |         |      
    Group 2 |   1     |         |   0.5   |
    Group 3 |  0.5    |  0.4    |   0.7   |

我的第一个问题是,我不能在我的模型Price(Prix)中保存2次Groupbread(我想用Group Bread Price的形式)。

第二个问题是,我不知道如何在我的模板中显示我的表格图表。我只能显示面包和可用的客户组。

有什么方法可以做到这一点吗?

非常感谢您的解答

python django templates model show
1个回答
0
投票

你可以做一个 ManyToManyField 之间 PainGroupe. 这个 ManyToManyField 拥有 through=… 模型[Django-doc]జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ Prix 模。

class Groupe(models.Model):
    nom = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name

class Pain(models.Model):
    nom = models.CharField(max_length=25, unique=True)
    pain_decouverte = models.BooleanField()
    groupes = models.ManyToManyField('client.Groupe', related_name='pains', through='facturation.PainPrix')

    def __str__(self):
        return self.nom

class PainPrix(models.Model):
    pain = models.ForeignKey('pain.Pain', related_name='prix', on_delete=models.CASCADE)
    groupe = models.ForeignKey('client.Groupe', related_name='prix', on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=4,decimal_places=2)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['pain', 'groupe'], name='prix_unique')
        ]
© www.soinside.com 2019 - 2024. All rights reserved.