예제 #1
0
 async def _process_insert_result(self, instance: Model, results: int) -> None:
     pk_field_object = self.model._meta.pk
     if (
         isinstance(pk_field_object, (SmallIntField, IntField, BigIntField))
         and pk_field_object.generated
     ):
         instance.pk = results
예제 #2
0
async def list_view(
        request: Request,
        model: Model = Depends(get_model),
        resources=Depends(get_resources),
        model_resource: ModelResource = Depends(get_model_resource),
        resource: str = Path(...),
        page_size: int = 10,
        page_num: int = 1,
):
    fields_name = model_resource.get_fields_name()
    fields_label = model_resource.get_fields_label()
    fields = model_resource.get_fields()
    qs = model.all()
    params, qs = await model_resource.resolve_query_params(
        request, dict(request.query_params), qs)
    filters = await model_resource.get_filters(request, params)
    total = await qs.count()
    if page_size:
        qs = qs.limit(page_size)
    else:
        page_size = model_resource.page_size
    qs = qs.offset((page_num - 1) * page_size)
    values = await qs.values(*fields_name)
    rendered_values, row_attributes, column_attributes, cell_attributes = await render_values(
        request, model_resource, fields, values)
    context = {
        "request": request,
        "resources": resources,
        "fields_label": fields_label,
        "fields": fields,
        "values": values,
        "row_attributes": row_attributes,
        "column_attributes": column_attributes,
        "cell_attributes": cell_attributes,
        "rendered_values": rendered_values,
        "filters": filters,
        "resource": resource,
        "model_resource": model_resource,
        "resource_label": model_resource.label,
        "page_size": page_size,
        "page_num": page_num,
        "total": total,
        "from": page_size * (page_num - 1) + 1,
        "to": page_size * page_num,
        "page_title": model_resource.page_title,
        "page_pre_title": model_resource.page_pre_title,
    }
    try:
        return templates.TemplateResponse(
            f"{resource}/list.html",
            context=context,
        )
    except TemplateNotFound:
        return templates.TemplateResponse(
            "list.html",
            context=context,
        )