'Django: Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead

I see this is a common problem and understand the idea is that you cannot add many to many unless it is created first. I have seen multiple solutions for this but unsure which one to use.

Models.py

class Concert(models.Model):
    venue = models.CharField(max_length=200, null=True)
    concertid = models.CharField(max_length = 200, null=False, default='didnotsaveproperly') 
    date = models.DateField(null=True) 
    city = models.CharField(max_length=100, null=True)
    country = models.CharField(max_length=200, null=True)
    user = models.ManyToManyField(USER_MODEL, related_name="concerts", null=True) 
    song = models.ManyToManyField("Song", blank=True, null=True)

    def __str__(self): 
        return str(self.concertid) + " " + str(self.venue) + " " + str(self.date) 

Views.py

def log_concert_and_song(request, concertdict):
    if request.method == "POST":
        Concert_save = Concert(
        concertid=concertdict['id'], 
        date=concertdict['eventDate'], 
        venue=concertdict['venue'], 
        city=concertdict['city'], 
        country=concertdict['country'], 
        user = request.user
        )
 
    Concert_save.save() 

Should I use the User model?



Solution 1:[1]

You have a misplaced closing parenthesis:

myNums = [len(myString[i] for i in range(len(myString)))]
                         ^                             |
                         |___should go there___________|

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alain T.