0
I’m trying to make a form in Django, in which I will save the versions of the projects.
Exemplo:
Empresa x
Projeto 1
Versão 1
Versão 2
Projeto 2
Versão 1
Empresa Y
Projeto 1a
Versão 1
...
It happens that when I add the project version, I need to select the company (X or Y) and the next field can only enable the projects in each company. If I put a fixed number (1, 2, .), the Django works, however, I need this number to be the result of the first field.
Follow the code I wrote:
class NovaEmpresa(models.Model):
title=models.CharField(max_length=200)
def __str__(self):
return self.title
class Projeto(models.Model):
empresa= models.ForeignKey(NovaEmpresa, on_delete=models.CASCADE)
sge=models.IntegerField()
local=models.CharField(max_length=200)
def __str__(self):
nome=str(self.empresa)+' (SGE: '+str(self.sge) +') - '+ str(self.local)
return nome
class Versao(models.Model):
empresa= models.ForeignKey(NovaEmpresa, on_delete=models.CASCADE)
variavel=int(empresa) #Problema está nessa linha, não aceita a conversão de ForeignKey para Int
projeto=models.ForeignKey(Projeto, on_delete=models.CASCADE, limit_choices_to={'empresa_id': variavel},)
#local=models.FileField()
def __str__(self):
return 'teste'
Some help?
You have to do this in javascript, see the end of my reply has a link to a complete Django project that answers your question, only instead of being a company/project it’s about make/model of car.
– Sidon
@André-Filipe top your code, I used in part (Html and View). Now I have the problem of this sequential number. Would it be possible that in this Versao class, I would add a filter in which I would pull how many lines there are into the selected project field, then I add 1 and use this result as a model.Integerfield? I wanted that when the user goes to add a new version, automatically the system already understands that it is the next version to previous (Example: Project is in version 3, when to add a new version, it already puts version 4).
– Bruno Sarante Faria