[英]Show value in table from a model with template in django
我想建立一個網站來管理面包生產,但我面臨兩個問題。
我有一個 model 面包(疼痛),它允許我制作面包並為該面包提供起始價格。 (nom_pain = 名稱,prix_HT = 價格)
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)
我有一個代表不同客戶組的 model 組(Groupe)。 (nom_groupe = 名稱)
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 |
我的第一個問題是我無法在我的 model 價格(Prix)中保存 2 次組/面包(我希望以組/面包/價格的形式保存)。
第二個問題是我不知道如何在我的模板中顯示我的表格圖表。 我只能顯示可用的面包和客戶組。
有沒有辦法做到這一點?
非常感謝您的回答
您可以在Pain
和Groupe
之間創建一個ManyToManyField
。 這個ManyToManyField
具有through=…
model [Django-doc] , Prix
model:
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')
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.