Django-rest-framework-datatables server side FieldError: Невозможно преобразовать ключевое слово '0' в поле
Я получаю эту ошибку:
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' в моем коде:
models.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)
serializers.py:
class UsersSerializer(serializers.ModelSerializer):
class Meta:
model = models.AuthUser
fields = ('first_name', 'last_name', 'email')
urls.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' ни в модели, ни в базе данных. Откуда оно может взяться?
Полагаю, вам нужно определить атрибут data-data
для каждого столбца (th
) или определить columns
при загрузке данных в DataTable()
Смотрите здесь.