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:
My idea in creating this structure is to restrict the inclusion of activities that are not related. Hence the intermediate class.
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.
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
– Neto Miranda
Yes, that’s why I put the limitation link. However, to consultation table, you can use as indicated.
– Ernesto Casanova
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!
– Neto Miranda
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.
– Ernesto Casanova
It took me a while but I came to put my solution. Thanks for the tip.
– Neto Miranda