I'm trying to build a dashboard app using django-plotly-dash. I was able to get the app up and running when pulling the data from a .CSV file but this is slow. To speed things up I imported the data into my backend Postgresql server and created a model in the pages app in my Django project. The problem comes when I attempt to import a specific table from my model into my stateless DjangDash app in a different folder. The error I keep getting is "unable to import pages.models"
Folder Structure:
DjangoProject
>dash_apps
>finished_apps
-appOne.py
>pages
-__init__.py
-admin.py
-apps.py
-models.py
-test.py
>base
-__init__.py
-asgi.py
-routing.py
-settings.py
-urls.py
-wsgi.py
>templates
>pages
-index.html
-manage.py
The app is named appOne.py
and the table or class is in the models.py
file and is named general_finance_metrics
So far I have tried the following in my appOne.py file with no luck:
from pages.models import general_finance_metrics
(error: unable to import 'pages.models' pylint (import-error))
from DjangoProject.pages.models import general_finance_metrics
(error: unable to import 'DjangoProject.pages.models' pylint (import-error))
from .models import general_finance_metrics
(error: attempted relative import beyond top-level package pylint (relative-beyond-top-level))
from base.models import general_finance_metrics
(error: unable to import 'base.models' pylint (import-error))
from base import models
(error: unable to import 'base' pylint (import-error))
from pages import models
(error: unable to import 'pages' pylint (import-error))
I dug through these other questions but wasn't able to find an answer that worked unless I overlooked it but I believe my situation is different from these because the DASH app is not inside of a single app but is inside of the DjangoProject.
Django : Unable to import model from another App
Django +2 ImportError: cannot import model
Can't get Python to import from a different folder
import django models classes into utility module
appOne.py code:
import numpy as np
import pandas as pd
import datetime
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
from django_plotly_dash import DjangoDash
from django.db import models
from pages.models import general_finance_metrics # error occurs here
df = pd.DataFrame(general_finance_metrics.objects.all().values())
models.py code:
class general_finance_metrics(models.Model):
columnOne = models.TextField(blank=True, null=True)
columnTwo = models.TextField(blank=True, null=True)
columnThree = models.TextField(blank=True, null=True)
def __str__(self):
return self.columnOne
admin.py code:
from django.contrib import admin
from .models import general_finance_metrics
urls.py code:
from django.urls import path
from dash_apps.finished_apps import appOne
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
index.html code:
<body>
{% load plotly_dash %}
<div class="{% plotly_class name='appOne' %} card" style="height: 100%; width: 100%;">
{% plotly_app name='appOne' ratio=0.95 %}
</div>
</body>