我正在做一个好友请求功能,我不能保存我的数据,这个错误每次都会出现。这是我的views.py
def sendrequest(request,receiver):
receiver_user = Profile.objects.get(username=receiver).username
sender=request.user
sender_user=Profile.objects.get(username=sender).username
connection=ConnectRequest(sender=sender_user,
receiver=receiver_user,
status="Pending"
)
connection.save()
return redirect("buddylist")这是我的Profile model
class Profile(models.Model):
idno=models.AutoField(primary_key=True)
name=models.CharField(max_length=30)
email=models.CharField(max_length=40)
username=models.CharField(max_length=30)这是我的ConnectRequest model
class ConnectRequest(models.Model):
sender = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="sender")
receiver = models.ForeignKey(Profile,on_delete=models.CASCADE, related_name="receiver")
choice=(
("Accepted","Accepted"),
("Declined","Declined"),
("Pending","Pending")
)
status=models.CharField(max_length=10,choices=choice,blank=True)
def __str__(self):
return f"{self.sender} to {self.receiver} status {self.status}"发布于 2020-11-06 03:38:01
sender_user=Profile.objects.get(username=sender).username
需要更改为
sender_user=Profile.objects.get(username=sender.username)
对于初学者来说。username需要是一个Charfield,但您给它提供的是一个用户实例request.user。
你需要改变
receiver_user = Profile.objects.get(username=receiver).username
至
receiver_user = Profile.objects.get(username=receiver)
因为当您将其传递给ConnectRequest时,它需要一个配置文件实例,而您为它提供了一个用户名
https://stackoverflow.com/questions/64704105
复制相似问题