私信  •  关注

ivissani

ivissani 最近创建的主题
ivissani 最近回复了
6 年前
回复了 ivissani 创建的主题 » 在django rest框架中重写cursorpagination的页面大小和顺序

您可以创建一个继承自 CursorPagination 类以设置自定义 page_size 和/或 max_page_size 像这样:

class CustomPageSizeCursorPagination(CursorPagination):
    page_size = 5
    max_page_size = 100

然后用这个类作为 pagination_class 视图集的字段

警告:以下代码未经测试

另一个选项是编写一个自定义分页器类,该类从视图集中获取页面大小。例如:

class PageSizeInViewSetCursorPagination(CursorPagination):
    def get_page_size(self, request, viewset_page_size):
        if self.page_size_query_param:
            try:
                return _positive_int(
                    request.query_params[self.page_size_query_param],
                    strict=True,
                    cutoff=self.max_page_size
                )
            except (KeyError, ValueError):
                pass

        return viewset_page_size or self.page_size

    def paginate_queryset(self, queryset, request, view=None):
        # Get the page_size from the viewset and then decide which page_size to use
        viewset_page_size = getattr(view, 'page_size', None) 
        page_size = self.get_page_size(request, viewset_page_size)


        # What follows is copy/paste of the code from CursorPagination paginate_queryset method
        if not self.page_size:
            return None

        self.base_url = request.build_absolute_uri()
        self.ordering = self.get_ordering(request, queryset, view)

        self.cursor = self.decode_cursor(request)
        if self.cursor is None:
            (offset, reverse, current_position) = (0, False, None)
        else:
            (offset, reverse, current_position) = self.cursor

        # Cursor pagination always enforces an ordering.
        if reverse:
            queryset = queryset.order_by(*_reverse_ordering(self.ordering))
        else:
            queryset = queryset.order_by(*self.ordering)

        # If we have a cursor with a fixed position then filter by that.
        if current_position is not None:
            order = self.ordering[0]
            is_reversed = order.startswith('-')
            order_attr = order.lstrip('-')

            # Test for: (cursor reversed) XOR (queryset reversed)
            if self.cursor.reverse != is_reversed:
                kwargs = {order_attr + '__lt': current_position}
            else:
                kwargs = {order_attr + '__gt': current_position}

            queryset = queryset.filter(**kwargs)

        # If we have an offset cursor then offset the entire page by that amount.
        # We also always fetch an extra item in order to determine if there is a
        # page following on from this one.
        results = list(queryset[offset:offset + self.page_size + 1])
        self.page = list(results[:self.page_size])

        # Determine the position of the final item following the page.
        if len(results) > len(self.page):
            has_following_position = True
            following_position = self._get_position_from_instance(results[-1], self.ordering)
        else:
            has_following_position = False
            following_position = None

        # If we have a reverse queryset, then the query ordering was in reverse
        # so we need to reverse the items again before returning them to the user.
        if reverse:
            self.page = list(reversed(self.page))

        if reverse:
            # Determine next and previous positions for reverse cursors.
            self.has_next = (current_position is not None) or (offset > 0)
            self.has_previous = has_following_position
            if self.has_next:
                self.next_position = current_position
            if self.has_previous:
                self.previous_position = following_position
        else:
            # Determine next and previous positions for forward cursors.
            self.has_next = has_following_position
            self.has_previous = (current_position is not None) or (offset > 0)
            if self.has_next:
                self.next_position = following_position
            if self.has_previous:
                self.previous_position = current_position

        # Display page controls in the browsable API if there is more
        # than one page.
        if (self.has_previous or self.has_next) and self.template is not None:
            self.display_page_controls = True

        return self.page

注意在上面的例子中 页尺寸 request 始终优先于您在代码中设置的任何内容。然后 viewset_page_size 是第二排最后一排 页尺寸 Pagination 班级。

6 年前
回复了 ivissani 创建的主题 » Django模型选择没有填充数据库

你误解了事情的运作方式, ModelForm 为了你 Report 模特会找的 ReportType 在html模板中预先填充select的实例。你需要创造 报告类型 首先是实例。

根据你的判断 报告类型 模型和你的问题我假设你认为django会创建一个 报告类型 为每个 REPORT_TYPE_CHOICES 但事实并非如此。这个 choices 字段中的属性用于验证。如果你想保持你的模型现在的样子,你需要创建一个 报告类型 报告类型选择 价值。

现在,除非你有充分的理由 报告类型 模特,你可以换你的 报告 按以下方式建模:

REPORT_TYPE_CHOICES = (
    ('E', 'Earnings'),
    ('MA', 'Monthly announcement'),
    ('WA', 'Weekly announcement'),
    ('SA', 'Sales announcement'),
)

class Report(models.Model):
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    report_type = models.CharField(
        max_length=50,
        choices=REPORT_TYPE_CHOICES,
        default="E"    # note we use the Key here
    )
    time_period = models.ForeignKey(ReportTimePeriod)
    link = models.URLField(max_length=500)
    report_conclusion = models.CharField(max_length=500, default="No conclusion yet")
    market_reaction = models.CharField(max_length=500, default="No market reaction yet")
6 年前
回复了 ivissani 创建的主题 » 为什么我的列表没有被python覆盖?[副本]

由于两个原因,您的列表未被覆盖,第一个原因是 strip 返回新字符串。第二个是当你这样做的时候 i = i.strip(whatever) 您没有覆盖 i 但是指向 一个新的值。因此,其他引用的旧值 不会因为你根本没有改变而受到影响。