I have a model defined like so:
class Vote(models.Model):
text = models.CharField(max_length=300)
voters = models.ManyToManyField(CustomUser, blank=True)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
I want a vote to automatically add all players of its associated game to its list of voters, when it is created. The Game
model has a method that returns its players, so I have overridden the save method of the Vote model:
def save(self, *args, **kwargs):
super().save(*args, **kwargs) #As the docs state it must be saved before m2m elements can be added
queryset = self.game.get_players
for player in queryset:
self.voters.add(player.id)
This does not work. It does not throw an error, and happily saves the model instance through the admin site. It does not, however, seem to add any of the players to the voters
field, (and the vote_voters table remains empty in the db).
Obvious troubleshooting: the queryset is definitely not empty, the save method is definitely being called.