Home > Net >  Duplicates with ManyToMany
Duplicates with ManyToMany

Time:02-02

I have model:

class GPD(models.Model):
    employee  = models.ForeignKey(Employee, verbose_name='Employee', on_delete = models.CASCADE, related_name=' ')
    gpd_year = models.PositiveIntegerField(default=2021, validators=[
            MaxValueValidator(2121),
            MinValueValidator(2021)
        ])
    corp_goal = models.ManyToManyField(CorporateGoal, blank=True)
    team_goal = models.ManyToManyField(TeamGoal, blank=True)
    ind_goal = models.ManyToManyField(IndividualGoal, blank=True)

def __str__(self):
        print('**********************')
        print(self.corp_goal.all())
        print('**********************')

        return 'GPD '   str(self.gpd_year)   ' for '   self.employee.name   ' '   self.employee.lastname

As you can see, I have many to many relationship and when I am trying to print(self.corp_goal.all()) I have duplicates: enter image description here

When I am using print(self.corp_goal.all().distinct()) - I have the same problem. How fix it?

CodePudding user response:

It works fine. I don't see duplicate, they are two different objects (Volume and Network capital). You just called it twice.

  •  Tags:  
  • Related