Limit choices to foreignkey in django rest framework

后端 未结 5 1110
半阙折子戏
半阙折子戏 2021-02-06 06:01

How to limit images of request.user to be linked with node. I wish I could do something like:

photo = models.ForeignKey(
    Image,
    limit_choices_to={\'owner         


        
5条回答
  •  无人共我
    2021-02-06 06:11

    from rest_framework import serializers
    
    class CustomForeignKey(serializers.PrimaryKeyRelatedField):
        def get_queryset(self):
            return Table.objects.filter(user=self.context['request'].user)
            # or: ...objects.filter(user=serializers.CurrentUserDefault()(self))
    
    class Serializer(serializers.ModelSerializer):
       table = CustomForeignKey()
    

提交回复
热议问题