Пример #1
0
    def get_url(self, obj, view_name, request, format):
        """
        Given an object, return the URL that hyperlinks to the object.

        May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
        attributes are not configured to correctly match the URL conf.
        """
        if hasattr(obj, 'pk') and obj.pk is None:
            return None

        if isinstance(obj, PKOnlyObject):
            obj = self.queryset.get(pk=obj.pk)

        parent_lookups = get_parent_query_lookups_by_view(
            view_name.split("-")[0])
        lookup_field = getattr(obj, self.lookup_field)
        kwargs = {self.lookup_field: lookup_field}
        if parent_lookups:
            for lookup in parent_lookups:
                parent_lookup = obj
                lookup_path = lookup.split('__')
                if len(lookup_path) > 1:
                    for part in lookup_path[:-1]:
                        parent_lookup = getattr(parent_lookup, part)
                parent_lookup_id = getattr(parent_lookup,
                                           lookup_path[-1] + '_id', None)
                kwargs[drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX +
                       lookup] = parent_lookup_id
        return reverse(view_name,
                       kwargs=kwargs,
                       request=request,
                       format=format)
Пример #2
0
    def get_url(self, obj, view_name, request, format):
        """
        Given an object, return the URL that hyperlinks to the object.

        May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
        attributes are not configured to correctly match the URL conf.
        """
        if hasattr(obj, 'pk') and obj.pk is None:
            return None

        parent_lookups = get_parent_query_lookups_by_view(
            view_name.split("-")[0])
        lookup_field = getattr(obj, self.lookup_field, None)
        kwargs = {self.lookup_field: lookup_field}
        if parent_lookups:
            for lookup in parent_lookups:
                lookup_path = lookup.split('__')
                parent_lookup = obj
                for part in lookup_path:
                    parent_lookup = getattr(parent_lookup, part)
                parent_lookup_id = parent_lookup.id if isinstance(
                    parent_lookup, Model) else parent_lookup
                kwargs[drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX +
                       lookup] = parent_lookup_id

        if lookup_field is None:  # Handle unsaved object case
            return None

        return reverse(view_name,
                       kwargs=kwargs,
                       request=request,
                       format=format)
Пример #3
0
    def get_url(self, obj, view_name, request, format):
        """
        Given an object, return the URL that hyperlinks to the object.

        May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
        attributes are not configured to correctly match the URL conf.
        """
        if hasattr(obj, 'pk') and obj.pk is None:
            return None

        if isinstance(obj, PKOnlyObject):
            obj = self.queryset.get(pk=obj.pk)

        parent_lookups = get_parent_query_lookups_by_view(view_name.split("-")[0])
        lookup_field = getattr(obj, self.lookup_field)
        kwargs = {self.lookup_field: lookup_field}
        if parent_lookups:
            for lookup in parent_lookups:
                parent_lookup = obj
                lookup_path = lookup.split('__')
                if len(lookup_path) > 1:
                    for part in lookup_path[:-1]:
                        parent_lookup = getattr(parent_lookup, part)
                parent_lookup_id = getattr(parent_lookup, lookup_path[-1] + '_id', None)
                kwargs[drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX + lookup] = parent_lookup_id
        return reverse(view_name, kwargs=kwargs, request=request, format=format)
Пример #4
0
    def get_url(self, obj, view_name, request, format):
        """
        Given an object, return the URL that hyperlinks to the object.

        May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
        attributes are not configured to correctly match the URL conf.
        """
        if hasattr(obj, 'pk') and obj.pk is None:
            return None

        parent_lookups = get_parent_query_lookups_by_view(view_name.split("-")[0])
        lookup_field = getattr(obj, self.lookup_field, None)
        kwargs = {self.lookup_field: lookup_field}
        if parent_lookups:
            for lookup in parent_lookups:
                lookup_path = lookup.split('__')
                parent_lookup = obj
                for part in lookup_path:
                    parent_lookup = getattr(parent_lookup, part)
                parent_lookup_id = parent_lookup.id if isinstance(parent_lookup, Model) else parent_lookup
                kwargs[drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX + lookup] = parent_lookup_id

        if lookup_field is None:  # Handle unsaved object case
            return None

        return reverse(view_name, kwargs=kwargs, request=request, format=format)
Пример #5
0
 def get_book_url(self, obj):
     request = self.context.get('request')
     endpoint = reverse('book-detail',
                        kwargs={
                            'project_pk': obj.project.pk,
                            'pk': obj.pk
                        },
                        request=request)
     return endpoint
Пример #6
0
 def get_fields_url(self, obj):
     request = self.context.get('request')
     endpoint = reverse('field-list',
                        kwargs={
                            'element_pk': obj.pk,
                            'project_pk': obj.project.pk
                        },
                        request=request)
     return endpoint