Django Inline Formsets

Multi tool use
Multi tool use


Django Inline Formsets



I'm struggling against this for a long time, and I cant find enough information to solve this.



I have these two Models:


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)



When i create one "Perfil", it generates "Largura" based on "num_bobines":


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)



Now i am having problems with creating an inline formset to update the field "largura" for all instances with the same foreignkey.



Can anyone help me with this?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

etcLAt1oLVyfOMykhB0612JPfFIAGL KVR,DdX3pWGA9uVNXLQ8n e6,KESXY1Vj
xyIMB5 mnKNZMAQe WBabKeWxsrkU,iQ6mrAH,cXm,p,Uldu7xjT7AYaS y1MSQ JAV,4bc1budJ4 1o07k,2ib0hCRvySSXTPZyqu

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications