📜  在 drf 中为 PrimaryKeyRelatedField 动态创建的查询集 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:24.230000             🧑  作者: Mango

代码示例1
class UserFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
    def get_queryset(self):
        request = self.context.get('request', None)
        queryset = super(UserFilteredPrimaryKeyRelatedField, self).get_queryset()
        if not request or not queryset:
            return None
        return queryset.filter(user=request.user)
      

class MySerializer(serializers.ModelSerializer):
    related = UserFilteredPrimaryKeyRelatedField(queryset=MyModel.objects)