Package: air-drf-relation
AIR-DRF-RELATIONTable of ContentsInstalationAboutAirRelatedFieldpk_onlyhiddenAirModelSerializeruserextra_kwargshidden_fieldsKwargs by actionsaction_read_only_fieldsaction_hidden_fieldsaction_extra_kwargsPriority extra_kwargsFilter nested querysetsInstalation$ pip install air-drf-relationAboutair-drf-relationadds flexibility and convenience in working with ModelSerializer.AirRelatedFieldUsed to extend the functionality of thePrimaryKeyRelatedFieldclassBookSerializer(ModelSerializer):# author = PrimaryKeyRelatedField(queryset=Author.objects) - default usageauthor=AirRelatedField(AuthorSerializer)city=AirRelatedField(CitySerializer)classMeta:model=Bookfields=('uuid','name','author','city')AirRelatedFieldallows you to get not only pk but also an object with pk, which will be searched.{"name":"demo","author":{"id":1},"city":1}pk_onlyAutomatically AirRelatedField returns a serialized object. If you only want to use pk, you must specify thepk_onlykey.author=AirRelatedField(AuthorSerializer,pk_only=True)hiddenHidden fields are not used for serialization and validation. The data will be returned without fields. Usually used together inAirModelSerializerauthor=AirRelatedField(AuthorSerializer,hidden=True)ImportantYou cannot usehiddenandpk_onlyin ModelSerializer and with extra_kwargsAirModelSerializerUsed to extend the functionality of theModelSerializerclassBookSerializer(AirModelSerializer):# full exampleauthor=AirRelatedField(AuthorSerializer)city=AirRelatedField(AuthorSerializer)classMeta:model=Bookfields=('uuid','name','author','city')hidden_fields=()read_only_fields=()# default read_only_fieldsextra_kwargs={}# default extra_kwargs with support custom keysaction_read_only_fields={'create':{},'_':{}# used for other actions},action_hidden_fields={'create':(),'_':()}action_extra_kwargs={'list':{},'_':{}}nested_save_fields=()userUser is automatically put from therequestif available. You can also set the user manually.classDemoSerializer(AirModelSerializer):classMeta:fields=('id','name')validate_name(self,value):ifnotself.user:returnNonereturnvalueManually set user.serializer=DemoSerializer(data={'name':'demo'},user=request.user)extra_kwargsExtends the standard work withextra_kwargsby adding work with additional attributes. You can also transferextra_kwargsmanually.classBookSerializer(AirModelSerializer):author=AirRelatedField(AuthorSerializer)classMeta:fields=('id','name','author')extra_kwargs={'author':{'pk_only':True},'name':{'hidden':True}}hidden_fieldsHides fields for validation and seralization.classBookSerializer(AirModelSerializer):classMeta:fields=('id','name','author')hidden_fields=('name','author')Kwargs by actionsKwargs by actions is used only when the event. You can pass acions separated by,.
For events that don't match, you can use_key. It is used if actionis passed.
Action is set automatically from the ViewSet, or it can be passed manually.classDemoViewSet(ModelViewSet):queryset=Demo.objects.all()serializer_class=DemoSerializerdefperform_create(serializer,request):action=serializer.action# action is 'create'serializer.save()@action(methods=['POST'],detail=False)defdemo_action(self,request):serializer=self.get_serializer_class()action=serializer.action# action is 'demo_action'Manually set action.serializer=DemoSerializer(data={'name':'demo'},action='custom_action')action=serializer.action# action is 'custom_action'action_read_only_fieldsSetsread_only_fieldsby action in serializer.classBookSerializer(AirModelSerializer):classMeta:fields=('id','name','author')action_read_only_fields={'create,update':('name','author')}action_hidden_fieldsSetshidden_fieldsby action in serializer.classBookSerializer(AirModelSerializer):classMeta:fields=('id','name','author')action_hidden_fields={'custom_action':('author',),'_':('id',)}action_extra_kwargsExpandextra_kwargsby action in serializer.classBookSerializer(AirModelSerializer):author=AirRelatedField(AuthorSerializer,pk_only=True,null=True)classMeta:fields=('id','name','author')action_extra_kwargs={'create,custom_update':{'author':{'pk_only':False,'null'=True}}}Priority extra_kwargsBelow are the priorities of the extra_kwargs extension in ascending orderextra_kwargsin Metahidden_fieldsaction_hidden_fieldsaction_read_only_fieldsaction_extra_kwargsextra_kwargsmanually transmittedFilter nested querysetsAirModelSerializer allows you to filter the queryset by nested fields.classBookSerializer(AirModelSerializer):city=AirRelatedField(CitySerializer,queryset_function_name='custom_filter')defqueryset_author(self,queryset):returnqueryset.filter(active=True,created_by=self.user)deffilter_city_by_active(self,queryset):returnqueryset.filter(active=True)classMeta:model=Bookfields=('uuid','name','author','city')