我需要得到如果请求用户在我的网站是否有会员资格,但我得到了一个错误,可以有人帮助我吗?用户会员模式为
class UserMembership(models.Model):
user = models.ForeignKey('accounts.User', on_delete=models.CASCADE, related_name='user_membership')
membership = models.ForeignKey(Membership, on_delete=models.DO_NOTHING, null=True, related_name='membership')
def __str__(self):
return self.user.email
序列化器是:
class UserMemberShipSerializer(serializers.ModelSerializer):
class Meta:
model = UserMembership
fields = ['id', 'user', 'membership']
和view
class MembershipView(viewsets.ModelViewSet):
model = UserMembership
serializer_class = UserMemberShipSerializer
def get_user_membership(self):
user_membership_qs = UserMembership.objects.get(user=self.request.user)
print(user_membership_qs)
if user_membership_qs.exists():
return user_membership_qs.first()
def get_queryset(self):
current_membership = self.get_user_membership(self.request)
return current_membership
和错误在这一行
current_membership = self.get_user_membership(self.request)
错误是current_membership = self.get_user_membership(self.request)TypeError: get_user_membership()接受1个位置参数,但是给了2个
删除" self.request ";从get_user_membership函数的输入:
current_membership = self.get_user_membership()
或添加"请求";作为函数参数:
def get_user_membership(self,request):
user_membership_qs = UserMembership.objects.get(user=request.user)
print(user_membership_qs)
if user_membership_qs.exists():
return user_membership_qs.first()