Limit choices to foreignkey in django rest framework

后端 未结 5 1075
半阙折子戏
半阙折子戏 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:
        (...)
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2021-02-06 06:24

    Because I am sure this logic will be used across an entire Django application why not make it more generic?

    class YourPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
    
        def __init__(self, **kwargs):
            self.model = kwargs.pop('model')
            assert hasattr(self.model, 'owner')
            super().__init__(**kwargs)
    
    
        def get_queryset(self):
            return self.model.objects.filter(owner=self.context['request'].user)
    

    serializers.py

    class SomeModelSerializersWithABunchOfOwners(serializers.ModelSerializer):
    
        photo = YourPrimaryKeyRelatedField(model=Photo)
        categories = YourPrimaryKeyRelatedField(model=Category,
                                                many=True)
        # ...
    
    
    0 讨论(0)
  • 2021-02-06 06:26

    I would deal with this by overriding get_serializer_class to dynamically return a serializer class at runtime, setting the choices option on the field there:

    def get_serializer_class(self, ...):
        user = self.request.user
        owner_choices = ...  # However you want to restrict the choices
    
        class ImageSerializer(serializers.ModelSerializer):
            owner = serializers.Field('owner.username', choices=owner_choices)
    
            class Meta:
                model = Image
                fields = ('file', 'owner')
    
        return ImageSerializer
    
    0 讨论(0)
  • 2021-02-06 06:26

    You can create a custom foreign key field and define get_queryset() method there to filter related objects to only those of your user. The current user can be retrieved from the request in the context:

    class UserPhotoForeignKey(serializers.PrimaryKeyRelatedField):
        def get_queryset(self):
            return Image.objects.filter(owner=self.context['request'].user)
    
    class NodeSerializer(serializers.HyperlinkedModelSerializer):
        photo = UserPhotoForeignKey()
        class Meta:
            model = Node
            fields = ('content', 'photo', 'owner')
    

    This example is using Django REST Framework version 3.

    0 讨论(0)
提交回复
热议问题