Django将用户添加到组错误“不是有效的UUID”

时间:2018-03-23 16:24:00

标签: django

抱歉我的英文。我想将用户添加到组,但我有自定义User whith id

id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)

然后我尝试将用户添加到自定义组:

user = User.objects.filter(email=request.user.email)
        my_group = Group.objects.get(name='test_group')
        my_group.user_set.add(user)

但有eroor

["'<QuerySet [<User: nesalexy@gmail.com>]>' is not a valid UUID."]

1 个答案:

答案 0 :(得分:3)

filter返回查询集 - 对象列表,但您需要传递给add单个对象,因此请使用get代替过滤器:

user = User.objects.get(email=request.user.email)
my_group = Group.objects.get(name='test_group')       
my_group.user_set.add(user)