我有这个模特
class Base(models.Model):
city = models.CharField(max_length=100)
district = models.CharField(max_length=100)
Owner = models.ForeignKey(User, related_name='%(class)s__Game_Owner')
attending = models.ManyToManyField(User)
requested = models.ManyToManyField(User, related_name="%(class)s__requested")
invited = models.ManyToManyField(User, related_name="%(class)s__invited")
teams = models.ManyToManyField(Team)
field = models.CharField(max_length=100)
Hash = models.CharField(max_length=32, blank=True)
我还有其他两个课程从这个基础开始。班级名称是 保留和招募 我在视图中创建了这个函数
def test_join_event(event_id, event_type, user_id):
event = None
if event_type =='enlisting':
event = enlistings.objects.get(id=event_id)
elif event_type =='reservation':
event = reservations.objects.get(id=event_id)
requested_user = User.objects.get(id=user_id)
if requested_user == event.Owner:
return "Error: you are already part of the event"
if requested_user in event.players.all():
return "Error 2!"
if requested_user in event.invited.all():
return "You are already invited to the event!"
if requested_user in event.requested.all():
return "You can't send your request again!"
event.requested.add(requested_user)
event.save()
return "Saved!!"
当我从翻译中调用该函数时,它一直在给我" Saved"但是它应该将用户添加到请求的列表中,并且下次我调用该函数时它将返回"您不能再次发送请求" ! 我从它返回的浏览器中尝试了该函数,但是当我查看解释器中的对象时,我看到请求列表为空(没有我刚刚添加的播放器)
当我从翻译中解脱时,我遇到了同样的问题!我只注意到我只能添加拥有该事件的用户。 例如像这段代码
event.requested.add(event.owner)
event.requested.all() # The user is added successfully !!
我无法理解为什么会这样。有任何想法吗??