Django-Tables2-Column-Shifter <frozen importlib._bootstrap> Ошибка при получении класса таблицы
Я успешно использую библиотеку Django-Tables2-Column-Shifter в своем Django-приложении из одного из моих приложений под названием app1. Я сталкиваюсь с причудливой ошибкой при попытке использовать ту же логику для использования Django-Tables2-Column-Shifter из другого приложения app2, которое использует другую модель. Ошибка, которую я получаю, выглядит следующим образом:
File "/home/datavizapp/.virtualenvs/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project_name'
Нижеприведенный код работает как ожидалось. Ошибка возникает в коде get_table_class, когда я изменяю следующее:
tabledata_table = get_table_1_class(
self.table_class_version
)(tabledata_queryset)
to
tabledata_table = get_table_2_class(
self.table_class_version
)(tabledata_queryset)
Ниже представлен "views.py" внутри app2
from project_name.app1.models import Table1
from project_name.app2.models import Table2
from project_name.app2.tables import get_table_1_class
from project_name.app2.tables import get_table_2_class
#from project_name.app1.tables import get_table_1_class # This also works if uncommented (original code is in app1)
from django_tables2_column_shifter.tables import (
ColumnShiftTableBootstrap4
)
from django.contrib.auth import get_user_model
User = get_user_model()
from django.views.generic import TemplateView
from django_tables2.config import RequestConfig
class Base(object):
container_css = "span10 offset1"
template_name = "cryptoscreener/shifter-template.html"
table_class_version = ColumnShiftTableBootstrap4
def get_context_data(self, **kwargs):
context = super(Base, self).get_context_data(**kwargs)
# Build tables
tabledata_queryset = Table1.objects.all()
#tabledata_queryset = Table2.objects.all() # This works fine
# Below is where the error is triggered. This code below works but if I change it to get_table_2_class I see the error
tabledata_table = get_table_1_class(
self.table_class_version
)(tabledata_queryset)
# Turn on sorting and pagination
RequestConfig(self.request, paginate={'per_page': 20}).configure(tabledata_table)
context['container_css'] = self.container_css
context['tabledata_table'] = tabledata_table
context['tabledata_queryset'] = tabledata_queryset
class Bootstrap5(Base, TemplateView):
container_css = "col-xs-10 col-xs-offset-1"
template_name = "cryptoscreener/shifter-template.html"
table_class_version = ColumnShiftTableBootstrap4
Ниже представлен "tables.py" внутри app2
from project_name.app1.models import Table1
def get_table_1_class(TableClass):
class Table1Class(TableClass):
class Meta:
model = Table1
return Table2Class
# The error appears when calling get_table_2_class from views.py in app2
from project_name.app2.models import Table2
def get_table_2_class(TableClass):
class Table2Class(TableClass):
class Meta:
model = Table2
return Table2Class
Ниже представлен шаблон "shifter-template.html"
{% load static %}
{% load django_tables2 %}
{% load render_table from django_tables2 %}
<!DOCTYPE html>
{% load static %}
<html lang="{{ LANGUAGE_CODE|default:"en" }}">
<head>
<title>Testproject</title>
<meta charset="utf-8">
<meta name="description" content="Testproject for django-tables2-column-shifter">
<meta name="author" content="Grzegorz Tężycki">
{# CSS for bootstrap #}
{% block bootstrap_css %}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
{% endblock %}
{% block extra_head %}{% endblock extra_head %}
<style>
body {
width : 90%;
margin: 0 auto;
}
</style>
</head>
<body>
{% render_table tabledata_table %}
{% block body_ekstra %}
{# Javascripts for bootstrap #}
{% block bootstrap_js %}
{% endblock %}
{# !! --> JS for column_shifter #}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.3/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script type="text/javascript" src="{% static 'django_tables2_column_shifter/js/django_tables2_column_shifter.min.js' %}"></script>
{% endblock body_ekstra %}
</body>
</html>
Я надеюсь, что у кого-то есть решение или предложение о том, как я могу дальше устранить эту проблему. Спасибо, что нашли время для обзора.