"ArrayList" in Django

1

I am developing an application for the generation of reports of fiber-optic certifications, and such reports are elaborated by importing XML and TXT files. The question is this:

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

My problem is precisely these Fibras_A and Fibras_B , because a snippet has ' Fibras_A and' Fibras_B , where 'x' and 'y' are passed at the time of creation of the Excerpt . I'm not getting, or rather, I'm not understanding how to create these ArrayLists to Fibras_A and Fibras_B , in class Trecho , depending on the value passed at creation time.

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 Fibra class, which should have the following attributes:

class Fibra(models.Model):

    id_fibra =
    distancia =
    subtrecho =
    statusFibra =

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

    
asked by anonymous 09.07.2018 / 23:34

1 answer

0

Create a foreign key of your Fiber class for Excerpt 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 have already the link between the 2 tables and the functions to get the shapes data together or separated, we just have to configure the creation

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

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

    
12.07.2018 / 21:07