我希望根据某些标准限制用户上传图片的类别。在下面的代码中,我能够实现我想要的网站功能。
class PhotoUploadForm(forms.ModelForm):
class Meta:
model = Photo
fields = ('photo', 'category')
def __init__(self, *args, **kwargs):
if not hasattr(self, 'request'):
self.request = kwargs.pop('request')
super(PhotoUploadForm, self).__init__(*args, **kwargs)
if not self.request.user.edu_email:
self.fields['category'].queryset = Category.objects.exclude(
title='University')
然而,我不知道怎样在Django Rest Framework中模仿这一点并在我的API中使用。请问有人知道如何做吗?
先感谢您的帮助!
序列化器:
class PhotoCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Photo
fields = [
'creator',
'photo',
'category',
]
视图:
class PhotoCreateAPIView(generics.CreateAPIView):
serializer_class = PhotoCreateSerializer