How to return attribute value X - in a Manytomanyfield

Asked

Viewed 115 times

0

I have some classes that relate to each other. Ex: I have an Activity class that has a M2m relationship with an Activity class. And I have another structure class that has a 1to1 with Activities_structures_complementary and with a second class Passive, where I do an inline inclusion in admin. This is my context and below are the respective codes.

Activity:

    class Atividade(models.Model):
        codigoSICRO = models.OneToOneField(Sicro, on_delete=models.CASCADE, unique=True, primary_key=True)
        nome = models.CharField('Nome', max_length=200, unique=True)
        descricao = models.TextField('Descrição', max_length=300, blank=True)
        unidadeMedida = models.ForeignKey(Unidade, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.nome

List of Activities of Complementary Structures:

    class Atividade_estruturas_complementares(models.Model):
        atividades_complementares = models.ManyToManyField(Atividade)

        class Meta:
            verbose_name_plural = 'Estruturas Complementares'
            verbose_name = 'Estruturas Complementares'

        def __str__(self):
            return self.atividades_complementares.nome

Estruturas Complementares:


    class Estruturas_complementares(models.Model):
    property = models.ForeignKey(Passivo,
                                 related_name='complementares',
                                 on_delete=models.CASCADE, null=True)
    atividade = models.OneToOneField(Atividade_estruturas_complementares, on_delete=models.PROTECT, related_name='atividade_estruturas_complementares')
    comprimento = models.DecimalField(
        'Comprimento', max_digits=10, decimal_places=2, blank=True, null=True, default=None)
    largura_altura = models.DecimalField(
        'Largura / Altura', max_digits=6, decimal_places=2, blank=True, null=True)
    profundidade = models.DecimalField(
        'Profundidade', max_digits=10, decimal_places=2, blank=True, null=True, default=None)

    class Meta:
        verbose_name = 'Estruturas Complementares'
        verbose_name_plural = 'Estruturas Complementares'

Faced with this situation in the class Activities_structures_complementary I would like to return the name of the activity that is in the activity class, but I’m not getting it, because I have no idea how to do this. The example:

    def __str__(self):
            return self.atividades_complementares.nome

returns the following error in tamplate:

inserir a descrição da imagem aqui

My idea in creating this structure is to restrict the inclusion of activities that are not related. Hence the intermediate class.

inserir a descrição da imagem aqui

In case you need any more information, I’m available. And as I am an intern and without any person in the area with experience to assist I am open to any kind of suggestion to better implement that solution.

2 answers

1

Good in M2m ratio, you can use it like this:

py.models

class Atividade_estruturas_complementares(models.Model):
        atividades_complementares = models.ManyToManyField(Atividade)

        class Meta:
            verbose_name_plural = 'Estruturas Complementares'
            verbose_name = 'Estruturas Complementares'

        @property
        def m2m_nome(self):
            return self.atividades_complementares.nome

admin py.

list_display = ('m2m_nome',)

NOTE: There are some limitations with Manyrelatedmanager, check here this topical.

  • in reality using M2m will not have to do what I want. I will have to use a 1to1. but thanks for the help anyway Ernesto

  • Yes, that’s why I put the limitation link. However, to consultation table, you can use as indicated.

  • I tried to use it the way you indicated, but in my case it didn’t solve the way I’d like. But it helped to better understand what I wanted and to devise a more efficient solution to my case. Thank you so much for your help!

  • Hey, share your solution so you can help someone in the future. Vote as useful my answer, if it has helped, even though it is not the solution.

  • It took me a while but I came to put my solution. Thanks for the tip.

0


Hello, my solution was instead of using M2m was to use 1to1 in the activity class:

class Atividade_estruturas_complementares(models.Model):
atividades_complementares = models.OneToOneField(Atividade, on_delete=models.PROTECT,
                                                 related_name='atividades_complementares')

class Meta:
    verbose_name_plural = 'Estruturas Complementares'
    verbose_name = 'Estruturas Complementares'

def __str__(self):
    return self.atividades_complementares.nome

Already in the Structures_complementary class I had to change the field from 1to1 to a foreignKey as you can see below.

class Estruturas_complementares(models.Model):
passivo_id = models.ForeignKey(Passivo,
                             related_name='complementares',
                             on_delete=models.CASCADE)
atividade_complementar = models.ForeignKey(Atividade_estruturas_complementares, on_delete=models.PROTECT,
                                 related_name='atividade_estruturas_complementares')
comprimento = models.DecimalField(
    'Comprimento', max_digits=10, decimal_places=2, blank=True, null=True, default=None)
largura_altura = models.DecimalField(
    'Largura / Altura', max_digits=6, decimal_places=2, blank=True, null=True)
profundidade = models.DecimalField(
    'Profundidade', max_digits=10, decimal_places=2, blank=True, null=True, default=None)

class Meta:
    verbose_name = 'Estruturas Complementares'
    verbose_name_plural = 'Estruturas Complementares'
    unique_together = (('passivo_id', 'atividade_complementar'),)

def __str__(self):
    return self.atividade_complementar.atividades_complementares.nome

that way it was possible to get what I would like.

Browser other questions tagged

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