"Arraylist" in Django

Asked

Viewed 104 times

1

I am developing an application for reporting optical fiber certifications, and such reports are produced by importing XML and TXT files. The question is as follows:

A certification is made within a "Section", which have the following attributes: (Localidade_A, Localidade_B, distance, Dio_A, Dio_B, Fibras_A, Fibras_B, among others).

My problem is right there Fibras_A and Fibras_B, because an excerpt has 'x' Fibras_A and 'y' Fibras_B, where 'x' and 'y' are passed at the time of the creation of the Excerpt. I’m not getting, or better, I’m not understanding how to create these ArrayLists for Fibras_A and Fibras_B, in class Trecho, depending on the value passed at the time of creation.

I have the class Trecho, which has the following attributes:

class Trecho(models.Model):

    rede = models.CharField("Rede*", max_length = 50, blank = False, choices = rede_choices)
    localidade_A = models.ForeignKey(Localidade, related_name = 'localA', on_delete = models.CASCADE)
    localidade_B = models.ForeignKey(Localidade, related_name = 'localB', on_delete = models.CASCADE)
    dio_A = models.CharField("Dio", max_length = 10, blank = True, null = True)
    dio_B = models.CharField("Dio", max_length = 10, blank = True, null = True)
    distancia = models.CharField("Distância de A para B (m)*", max_length = 9, blank = False, null = True)

And a class Fibra, which must have the following attributes:

class Fibra(models.Model):

    id_fibra =
    distancia =
    subtrecho =
    statusFibra =

A screen print for you to understand. When I click on salvar the system shall create 24 fibras_A and 36 fibras_B.

print

1 answer

0

Creates a foreign key of your Fiber to Stretch class and a new column to indicate the type of that fiber, whether it is A or B

 TIPOA = 'A'
    TIPOB = 'B'
    TIPO_FIBRA_CHOICES = (
        (TIPOA, 'Tipo A'),
        (TIPOB, 'Tipo B'),
    )
    class Trecho(models.Model):
        ...
        def obter_fibras_tipoA(self):
            return self.fibra_trecho.filter(tipo_fibra=TIPOA)

        def obter_fibras_tipoB(self):
            return self.fibra_trecho.filter(tipo_fibra=TIPOB)

        def obter_todas fibras(self):
            return self.fibra_trecho.all()

    class Fibra(models.Model):
        ...
        tipo_fibra = models.CharField(choices=TIPO_FIBRA_CHOICES)
        trecho = models.ForeignKey(Trecho, related_name="fibra_trecho")

Now that we already have the link between the 2 tables and the functions to get the data together or separate forms only remains to configure the creation

  Fibra.objects.create(tipo_fibra=TIPOA, ...) # ... significa o resto dos atributos
  Fibra.objects.create(tipo_fibra=TIPOB, ...)

is just create each fiber passing its type and the rest of its attributes, now the logic of creation to differentiate 1 fiber from the other to simply not create 34 exactly equal objects, there depends on you and how you want to do it

  • Thank you very much, I’ll try to do it here.

  • It worked out, thank you very much my friend, it saved my day

Browser other questions tagged

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