Django - Questions with Foreignkey

Asked

Viewed 542 times

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.

  • @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).

2 answers

0

I’m analyzing your architecture. If the same project cannot be in more than one company and a version belongs to only one project, it is possible to eliminate the "company" field of the Version object. Making it unnecessary to check the project company in the version.

  • In that case, it’s necessary. Imagine that I want to add Versao 2 to project 1a, for that, I just find the project 1a in the registration list (Admin). What happens is that it is the version number (1, 2, sequential), it cannot be entered by the user (Integerfield field), because otherwise, it can occur of it create two versions 1 for the same project, what would be a system failure.

0

I solved the problem.

It is possible to execute a function before saving, so the code was:

class Versao(models.Model):
    projeto=models.ForeignKey(Projeto, on_delete=models.CASCADE)
    local=models.FileField()
    num_versao=models.IntegerField(blank=True)

    def __str__(self):
        return str(self.projeto) + '_V' + str(self.num_versao)

    def save(self, force_update=False, force_insert=False):
        qtd_versoes=Versao.objects.filter(projeto=self.projeto).count()
        self.num_versao=qtd_versoes+1
        super(Versao, self).save(force_insert, force_update)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.