Limit choices to foreignkey in django rest framework

后端 未结 5 1108
半阙折子戏
半阙折子戏 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:07

    class CustomForeignKey(serializers.PrimaryKeyRelatedField):
        def get_queryset(self):
            return Table.objects.filter(is_active=True)
    
    class Serializer(serializers.ModelSerializer):
        (...)
       table= CustomForeignKey()
       class Meta:
       (...)
    

    even more easy is :

    class Serializer(serializers.ModelSerializer):
        (...)
        table = serializers.PrimaryKeyRelatedField(queryset=Table.objects.filter(is_active=True)) 
        class Meta:
        (...)
    

提交回复
热议问题