我有一个序列化程序:
class MySerializer(serializers.ModelSerializer): class Meta: model = models.MyClass fields = "__all__" def validate(self, data): user = self.context.get("request").user users = data.get("users") users_list = User.objects.filter(organization=user.organization) return data
用户将打印如下模型列表:[, ]
users_list 将显示一个查询集: Queryset: , \, \]>
我想编写一个查询来检查模型列表 egusers 是否存在于查询集 users_list 中。怎么做?
正如我所见,您需要将每个User组织与request.user组织进行比较。如果你有对象列表,你可以不用QuerySet:
User
request.user
QuerySet
user = self.context.get("request").user users = data.get("users") for u in users: if u.organization == user.organization: # do what you want if that's True or anything return data