Django:禁止直接赋值给多对多集合的前端。使用 user.set() 代替
Django: Direct assignment to the forward side of a many-to-many set is prohibited. Use user.set() instead
我看到这是一个常见问题,我理解的想法是,除非先创建它,否则不能将多对多相加。我已经看到了多种解决方案,但不确定使用哪一种。
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()
我应该使用用户模型吗?
您可以使用 add
添加到 Many-to-Many 关系
阅读文档:https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/#many-to-many-relationships
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'],
)
Concert_save.save()
Concert_save.user.add(request.user)
或如错误所说:
list_of_users = [request.user,]
Concert_save.user.set([list_of_users])
我看到这是一个常见问题,我理解的想法是,除非先创建它,否则不能将多对多相加。我已经看到了多种解决方案,但不确定使用哪一种。
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()
我应该使用用户模型吗?
您可以使用 add
添加到 Many-to-Many 关系
阅读文档:https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_many/#many-to-many-relationships
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'],
)
Concert_save.save()
Concert_save.user.add(request.user)
或如错误所说:
list_of_users = [request.user,]
Concert_save.user.set([list_of_users])