Django内嵌表单集

时间:2018-07-02 12:03:13

标签: python django django-forms inline-formset

很长一段时间以来,我一直在为此奋斗,但我找不到足够的信息来解决这个问题。

我有以下两个模型:

class Perfil(models.Model):
    CORE = (('3', '3'),('6', '6'))
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Username")
    timestamp = models.DateTimeField(auto_now_add=True)
    nome = models.CharField(verbose_name="Perfil", max_length=200, unique=True, null=True, blank=True )
    num_bobines = models.PositiveIntegerField(verbose_name="Número de bobines")
    largura_bobinagem = models.DecimalField(verbose_name="Largura da bobinagem", max_digits=10, decimal_places=2)
    core = models.CharField(verbose_name="Core", max_length=1, choices=CORE)
    gramagem = models.DecimalField(verbose_name="Gramagem", max_digits=10, decimal_places=2)
    espessura = models.DecimalField(verbose_name="Espessura", max_digits=10, decimal_places=2)
    densidade_mp = models.DecimalField(verbose_name="Densidade da matéria prima", max_digits=10, decimal_places=2)
    velocidade = models.DecimalField(verbose_name="Velocidade", max_digits=10, decimal_places=2)
    producao = models.DecimalField(verbose_name="Produção", max_digits=10, decimal_places=2)
class Meta:
    verbose_name_plural = "Perfis"
    ordering = ['-timestamp']

def __str__(self):
    return '%s' % (self.nome)

class Largura(models.Model):
    perfil = models.ForeignKey(Perfil, on_delete=models.CASCADE, verbose_name="Largura")
    num_bobine = models.PositiveIntegerField(verbose_name="Bobine nº")
    largura = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)


class Meta:
    verbose_name_plural = "Larguras"
    ordering = ['perfil']

def __str__(self):
    return '%s - Bobine nº: %s, %s mm' % (self.perfil, self.num_bobine, self.largura)

当我创建一个“ Perfil”时,它会基于“ num_bobines”生成“ Largura”:

def perfil_larguras(sender, instance, **kwargs):
    for i in range(instance.num_bobines):
        lar = Largura.objects.create(perfil=instance, num_bobine=i+1)
        lar.save()

post_save.connect(perfil_larguras, sender=Perfil)

现在我在创建内联表单集以为具有相同外键的所有实例更新字段“ largura”时遇到问题。

有人可以帮我吗?

0 个答案:

没有答案