我正在尝试让一个朋友请求在Django项目上使用基于类的视图,但是却不断出错。我目前收到的错误是: 在/ friend_request / joe处发生TypeError int()参数必须是字符串,类似字节的对象或数字,而不是'User'
虽然我的朋友模型不是在寻找int(),所以我不确定发生了什么。
基于类的视图:
class AddFriend(APIView):
def get(self, request, username):
friend = User.objects.get(username=username)
current_user = request.user.username
f = Friend(current_user, friend, 1)
f.save()
url:
url(r'^friend_request/(?P<username>[\w.@+-]+)', AddFriend.as_view(), name = 'add_friend'),
型号:
class Friend(models.Model):
#static variables for friend_status attribute
FRIENDS = 1
A_REQUESTS_B = 2
B_REQUESTS_A = 3
friend_A = models.ForeignKey(User, related_name='friend_A')
friend_B = models.ForeignKey(User, related_name='friend_B')
friend_status = models.IntegerField()
def __str__(self):
return '%s and %s friendship' % (self.friend_A, self.friend_B)
class Meta:
unique_together = (('friend_A', 'friend_B'),)
理想情况下,此CBV将找出发送请求的用户是谁,在URL中找出正在“查找”的朋友,并创建/保存新的朋友实例。这个int()错误从哪里来,我该如何解决?