抱歉我的英文。我想将用户添加到组,但我有自定义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."]
答案 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)