gpt4 book ai didi

python - 类型错误 : Field 'id' expected a number but got

转载 作者:行者123 更新时间:2023-12-04 17:26:24 25 4
gpt4 key购买 nike

Cannot cast AnonymousUser to int.
Are you trying to use it in place of User?

当我向我的 url 发出发布请求时,我不确定如何调整我的代码以停止生成此错误。我附上了我的序列化程序、 View 和模型的代码。
class ActivitySessionSerializer(serializers.Serializer):
activity_name = serializers.CharField(min_length=1, max_length=100)

def create(self, validated_data):
activity, created = Activity.objects.get_or_create(name=validated_data['activity_name'],
owner=self.context['request'].user)

return ActivitySession.objects.create(activity=activity, start_time=datetime.datetime.now())



class StartSessionView(generics.CreateAPIView):
serializer_class = serializers.ActivitySessionSerializer


User = get_user_model()

class ActivityType(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User, on_delete=models.CASCADE)

class Activity(models.Model):
name = models.CharField(max_length=100)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
activity_type = models.ForeignKey(ActivityType, on_delete=models.CASCADE, null=True, blank=True)

class ActivitySession(models.Model):
activity = models.ForeignKey(Activity, on_delete=models.CASCADE)
start_time = models.DateTimeField()
end_time = models.DateTimeField()


urlpatterns = [
path('starting_session/', views.StartSessionView.as_view()),
]
我相信我是通过换线解决的
Activity.objects.get_or_create(name=validated_data['activity_name'], 
owner=self.context['request'].user)
Activity.objects.get_or_create(name=validated_data['activity_name'], 
owner=self.context['request'].user.id)
我必须专门添加 id 字段而不是总用户对象

最佳答案

AnonymousUserid=Nonepk=None ,所以在这种情况下不能直接使用。相反,建议是 create an instance of user that reflects an anonymous user .

关于python - 类型错误 : Field 'id' expected a number but got <django. contrib.auth.models.AnonymousUser 对象在 0x048C7E90>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62966136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com