django-rest-framework-datatables 服务器端 FieldError:无法将关键字“0”解析为字段

问题描述 投票:0回答:1

我已经关注了这里的文档:https://django-rest-framework-datatables.readthedocs.io/

我收到此错误:

django.core.exceptions.FieldError: Cannot resolve keyword '0' into field. Choices are: adjustmentsheader, authusergroups, authuseruserpermissions, brand, brand_modified_by_set, brandcategory, brandcategory_modified_by_set, company, company_modified_by_set, customer, customer_modified_by_set, customerbranch, customerbranch_modified_by_set, customerproduct, customerproduct_modified_by_set, customertype, customertype_modified_by_set, damageheader, date_joined, djangoadminlog, drfheader, drfheader_approved_by_set, drfheader_cancelled_by_set, drfheader_modified_by_set, email, first_name, id, is_active, is_staff, is_superuser, last_login, last_name, password, product, product_modified_by_set, productsupplier, productsupplier_modified_by_set, pulloutheader, pulloutheader_approved_by_set, receivingheader, receivingheader_modified_by_set, supplier, supplier_modified_by_set, username

但是我的代码中没有关键字“0”:

模型.py

class AuthUser(models.Model):
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    email = models.CharField(max_length=254)

序列化器.py:

class UsersSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.AuthUser
        fields = ('first_name', 'last_name', 'email')

url.py

router = routers.DefaultRouter()
router.register(r'user_viewset', views.UsersViewSet)


urlpatterns = [
    path('api/', include(router.urls)),

views.py

class UsersViewSet(viewsets.ModelViewSet):
    queryset = models.AuthUser.objects.all().order_by("last_name")
    serializer_class = serializers.UsersSerializer

模板:

            <div class="table-responsive-xl">
                <table class="table table-hover table-striped table-sm" id="table1">
                    <thead class="sticky-top top-0">
                        <tr>
                            <th>Item Code</th>
                            <th>Customer Barcode</th>
                            <th>Brand</th>
                            <th>Category</th>
                            <th>Description</th>
                            <th>Customer Price</th>
                        </tr>
                    </thead>
                </table>
            </div>


        </div>

        <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
        <script src="//cdn.datatables.net/2.0.8/js/dataTables.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#table1').DataTable({
                    "serverSide": true,
                    "ajax": "../api/product_viewset/?format=datatables"
                });
            });
        </script>

我的模型或数据库中没有关键字“0”。它可能从哪里来?

回溯:

[17/Jul/2024 15:26:47] "GET /poc/customer/830 HTTP/1.1" 200 14452
Internal Server Error: /poc/api/user_viewset/
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python39\lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
  File "C:\Python39\lib\site-packages\rest_framework\viewsets.py", line 124, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Python39\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Python39\lib\site-packages\rest_framework\mixins.py", line 38, in list
    queryset = self.filter_queryset(self.get_queryset())
  File "C:\Python39\lib\site-packages\rest_framework\generics.py", line 154, in filter_queryset
    queryset = backend().filter_queryset(self.request, queryset, self)
  File "C:\Python39\lib\site-packages\rest_framework_datatables\filters.py", line 194, in filter_queryset
    queryset = queryset.order_by(*ordering)
  File "C:\Python39\lib\site-packages\django\db\models\query.py", line 1659, in order_by
    obj.query.add_ordering(*field_names)
  File "C:\Python39\lib\site-packages\django\db\models\sql\query.py", line 2221, in add_ordering
    self.names_to_path(item.split(LOOKUP_SEP), self.model._meta)
  File "C:\Python39\lib\site-packages\django\db\models\sql\query.py", line 1724, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword '0' into field. Choices are: adjustmentsheader, authusergroups, authuseruserpermissions, brand, brand_modified_by_set, brandcategory, brandcategory_modified_by_set, company, company_modified_by_set, customer, customer_modified_by_set, customerbranch, customerbranch_modified_by_set, customerproduct, customerproduct_modified_by_set, customertype, customertype_modified_by_set, damageheader, date_joined, djangoadminlog, drfheader, drfheader_approved_by_set, drfheader_cancelled_by_set, drfheader_modified_by_set, email, first_name, id, is_active, is_staff, is_superuser, last_login, last_name, password, product, product_modified_by_set, productsupplier, productsupplier_modified_by_set, pulloutheader, pulloutheader_approved_by_set, receivingheader, receivingheader_modified_by_set, supplier, supplier_modified_by_set, username
[17/Jul/2024 15:26:48] "GET /poc/api/user_viewset/?format=datatables&draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&order%5B0%5D%5Bname%5D=&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1721201207990 HTTP/1.1" 500 165912
django serialization django-rest-framework datatables
1个回答
0
投票

我想你需要在每列(

data-data
)上定义
th
属性,或者在
columns
中加载数据时定义
DataTable()

请参阅此处

© www.soinside.com 2019 - 2024. All rights reserved.