Server IP : 85.214.239.14 / Your IP : 3.148.104.103 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/self/root/srv/modoboa/env/lib64/python3.5/site-packages/rest_framework_nested/ |
Upload File : |
import rest_framework.serializers from rest_framework_nested.relations import NestedHyperlinkedIdentityField try: from rest_framework.utils.field_mapping import get_nested_relation_kwargs except ImportError: pass # passing because NestedHyperlinkedModelSerializer can't be used anyway # if version too old. class NestedHyperlinkedModelSerializer(rest_framework.serializers.HyperlinkedModelSerializer): """ A type of `ModelSerializer` that uses hyperlinked relationships with compound keys instead of primary key relationships. Specifically: * A 'url' field is included instead of the 'id' field. * Relationships to other instances are hyperlinks, instead of primary keys. NOTE: this only works with DRF 3.1.0 and above. """ parent_lookup_kwargs = { 'parent_pk': 'parent__pk' } serializer_url_field = NestedHyperlinkedIdentityField def __init__(self, *args, **kwargs): self.parent_lookup_kwargs = kwargs.pop('parent_lookup_kwargs', self.parent_lookup_kwargs) super(NestedHyperlinkedModelSerializer, self).__init__(*args, **kwargs) def build_url_field(self, field_name, model_class): field_class, field_kwargs = super(NestedHyperlinkedModelSerializer, self).build_url_field( field_name, model_class ) field_kwargs['parent_lookup_kwargs'] = self.parent_lookup_kwargs return field_class, field_kwargs def build_nested_field(self, field_name, relation_info, nested_depth): """ Create nested fields for forward and reverse relationships. """ class NestedSerializer(NestedHyperlinkedModelSerializer): class Meta: model = relation_info.related_model depth = nested_depth - 1 fields = '__all__' field_class = NestedSerializer field_kwargs = get_nested_relation_kwargs(relation_info) return field_class, field_kwargs